Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

WiFi library getSocket function incorrect

Open SurferTim opened this issue 12 years ago • 6 comments

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;
}

SurferTim avatar Dec 11 '13 11:12 SurferTim

If I write this code in file WiFi.cpp, the WiFiUDP not working well..

BernatLoscos avatar Jan 03 '14 20:01 BernatLoscos

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.

SurferTim avatar Jan 03 '14 21:01 SurferTim

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.

BernatLoscos avatar Jan 03 '14 21:01 BernatLoscos

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.

SurferTim avatar Jan 03 '14 21:01 SurferTim

You did upgrade the wifi shield firmware to 1.1.0, correct?

SurferTim avatar Jan 03 '14 21:01 SurferTim

This issue was moved to arduino-libraries/WiFi#4

agdl avatar Jul 12 '16 13:07 agdl