Skip to content

Commit c6a8da6

Browse files
authoredNov 6, 2020
Allow faster reuse of socket, to be able to restart WifiServer. (#4306)
See #3960 for more details of the problem and the solution. I only implemented what was proposed in this ticket, as it solves my problem, which was the same as in this ticket. Credits for the code going to @etrinh ;-) This also is a more consistence behaviour compared to esp8266, where it also is possible to restart the wifiserver immediately on the same port.
1 parent dd1a154 commit c6a8da6

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed
 

‎libraries/WiFi/src/WiFiServer.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ WiFiClient WiFiServer::available(){
6363
}
6464

6565
void WiFiServer::begin(uint16_t port){
66+
begin(port, 1);
67+
}
68+
69+
void WiFiServer::begin(uint16_t port, int enable){
6670
if(_listening)
6771
return;
6872
if(port){
@@ -72,6 +76,7 @@ void WiFiServer::begin(uint16_t port){
7276
sockfd = socket(AF_INET , SOCK_STREAM, 0);
7377
if (sockfd < 0)
7478
return;
79+
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
7580
server.sin_family = AF_INET;
7681
server.sin_addr.s_addr = INADDR_ANY;
7782
server.sin_port = htons(_port);

‎libraries/WiFi/src/WiFiServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class WiFiServer : public Server {
4040
WiFiClient available();
4141
WiFiClient accept(){return available();}
4242
void begin(uint16_t port=0);
43+
void begin(uint16_t port, int reuse_enable);
4344
void setNoDelay(bool nodelay);
4445
bool getNoDelay();
4546
bool hasClient();

0 commit comments

Comments
 (0)
Please sign in to comment.