WiFi library getSocket function incorrect
The way the getSocket() function allocates sockets is incorrect. It uses the wrong array to determine availability. This will cause errors if using a client connection with any of the listening protocols (udp and tcp server). This is in WiFi.cpp. IDE v1.0.5 and v1.5.5.
uint8_t WiFiClass::getSocket()
{
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
{
if (WiFiClass::_server_port[i] == 0)
{
return i;
}
}
return NO_SOCKET_AVAIL;
}
This is the function I currently use, and it works good. It uses the state array instead. This is the same variable array that the client socket allocation uses.
uint8_t WiFiClass::getSocket()
{
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
{
if (WiFiClass::_state[i] == 0)
{
return i;
}
}
return NO_SOCKET_AVAIL;
}
If I write this code in file WiFi.cpp, the WiFiUDP not working well..
Be more specific about "not working well". I just finished a test of the UDP part of the library. My test sketch ran fine for over a week and sent/received over 150,000 packets with this change.
If you stop the UDP socket and try to restart it, it will not start the socket listening again. That is a different problem than this. That is issue #1637.
Now I cannot use WifiUDP, For example, If I send UDP packet for my mobile App, the server UDP not answer my packet. I try this issue #1637.
It worked with the previous code before the change above? Mine worked with the old code too until I tried using the UDP with a client, and the client took the same socket as the udp.
You cannot stop and start udp. It causes a fail, and the socket will not respond.
Post your problem and code on the forum in the networking section.
You did upgrade the wifi shield firmware to 1.1.0, correct?
This issue was moved to arduino-libraries/WiFi#4