I've got some code using the WiFiServer class and it's working wonderfully. I'd like to use the same pattern, but with IPv6. There doesn't seem to be anything to do with IPv6 in the Arduino WiFi Server API, and I couldn't find an IPv6 server implementation online, so I tried to create an IPv6 port of the existing WiFiServer class, and by that I mean I copied the class, changed the two occurrences of AF_INET to AF_INET6, and called it WiFiServerV6. Unfortunately, it didn't work.
void WiFiServerV6::begin(uint16_t port, int enable){
if(_listening)
return;
if(port){
_port = port;
}
struct sockaddr_in server;
sockfd = socket(AF_INET6 , SOCK_STREAM, 0);
if (sockfd < 0)
return;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
server.sin_family = AF_INET6;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(_port);
if(bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
return;
if(listen(sockfd , _max_clients) < 0)
return;
fcntl(sockfd, F_SETFL, O_NONBLOCK);
_listening = true;
_noDelay = false;
_accepted_sockfd = -1;
}
(Modified from WiFiServer.cpp in the ESP32 Arduino library)
Admittedly, this was a shot in the dark.
I've checked the return codes of setsockopt
and fcntl
, and they come back clean, so there doesn't seem to be an issue there.
Before calling begin
, I connect to Wi-Fi, call WiFi.enableIpV6()
, retrieve the v6 address with WiFi.localIPv6()
and all seems well. I am able to ping the given address just fine, so it's just the server that isn't working. Is there an implementation or example of an IPv6 TCP server for ESP32? Or, do you have any recommendations for rectifying my port? Thank you.