Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

WiFi server uses only one socket

Open SurferTim opened this issue 13 years ago • 5 comments

The wifi shield library apparently uses only one socket, and that one poorly. The function below displays the state of each of the 4 sockets, and the port number if a server.

The first number is the status of that socket. -1 = available 0 to 3 = used

The second is the port.

When I start the server and call this function, I get this 0 80 -1 0 -1 0 -1 0

After the first request from a client, I get this -1 80 -1 0 -1 0 -1 0

That is indicating to me the server is not starting a second socket listening when a client connects on the first as does the ethernet shield.

Also note that socket 0 is now available after the first request. That seems like a bug to me.

I would expect this after the first client request -1 0 1 80 -1 0 -1 0

I am getting wrong image downloads and corrupted images using the server library. I think this may be related to the use of only one socket.

Or am I seeing this incorrectly?

void ShowSockStatus() {
  for(int x = 0; x < MAX_SOCK_NUM; x++) {
    Serial.print(WiFi._state[x]);    
    Serial.print("  ");
    Serial.println(WiFi._server_port[x]);    
  }
}

SurferTim avatar Dec 03 '13 13:12 SurferTim

Why is there not a function to start the server socket listening again? If the socket is a tcp server, then it should not be listening for another connection until after client.stop() is called. This is from WiFiClient.cpp in the stop() function. The server library uses this same function to close a server connection also. It has my bug fix there to prevent releasing the socket if it is a server (listening) socket.

// add this line to prevent release of a listening socket (server)
  if(WiFiClass::_server_port[_sock] == 0)
    WiFiClass::_state[_sock] = NA_STATE;
// add this line so if a server socket, start the server socket listening again
  else WiFiServer::listen(_sock);

I could not find a listen() function in WiFiServer, but I should see one. The server.available() function evaluating to "true" should have stopped the socket from listening for another connection. It should resume listening after client.stop().

SurferTim avatar Dec 14 '13 12:12 SurferTim

I manage to serve two HTTP connections on two different ports with persistent connections with this code:

WiFiServer server[] = { WiFiServer(81),
                        WiFiServer(80) }; // web servers
const byte serverCount = sizeof(server) / sizeof(*server);

void webServer ()
{
  for (byte ci = 0; ci < serverCount; ++ci)
    server[ci].begin();

  WiFiClient client[serverCount];

  // Forever loop on one or two seb servers
  for (byte ci = 0; true; ci = serverCount - 1 - ci)
    {
      if (client[ci])
    serveWebConnection(client[ci]);
      else
    {
      client[ci] = server[ci].available();
      client[ci].setTimeout(5000);
    }

      delay(50);
    }
}

void serveWebConnection(Client& client)
{
  char command[8];
  byte nr;

  if (client.connected())
    {
      nr = client.readBytesUntil('/', command, sizeof(command));
      command[min(7,nr)] = '\0';

      if (nr > 0
      && strncmp_P(command, PSTR("GET "), 4) == 0)
    {
      nr = client.readBytesUntil('\r', command, sizeof(command));
      command[min(7,nr)] = '\0';

      if (nr > 0)
        {
          // It is important to answer before reading the whole command, not after:
          // it works both ways but this way it is much faster with the WiFi shield
          // probably because of some timing internal to the shield.
          answerClient(client, command); // answer before reading the whole request

          char endOfHeaders[] = "\r\n\r\n";
          if (client.find(endOfHeaders))
        return;
        }
    }
    }

  // Something went wrong: close the connection
  delay(1);
  client.stop();
}

However the thing is unstable, and I do not understand if this is because I have some problems in my other code or problems in the WIFi library, still working on this.

The really ugly thing is that I get data corruption when sending the HTTP data response. One of my responses is a long (about 1kB) string which contains a web page. Depending on the length and the contents of this string, it may get corrupted: one, two or three characters in fixed positions in the string are corrupted before transmission. To work around this, I randomly add white space or comment strings: the number and position of the corrupted characters change in an apparently random fashion when changing the string until finally the problem goes away. My investigations point in the direction of the SPI communication with the WiFi shield or the firmware inside the shield itself. I'll file a bug report as soon as I find the time to create a reproducible test case.

fpoto avatar Dec 16 '13 12:12 fpoto

This is the function I use to check the status of the sockets. It displays 5 numbers per socket. The first two are in the Arduino memory and represent the state and server port used to allocate sockets. The next three are the server status, client status, and data status from the wifi shield.

The state is -1 if the socket is available and the socket number (0 to 3) if used. The server port is the port used if that socket is a server.

The 's' is the server status. It is zero if not a server, and one if it is tcp listener (server). The 'c' is the client state. It is zero if no client connected, and 4 if connected. The 'd' is the data state. It is zero if no characters in the rx buffer, or the count if there are.

#include <utility/server_drv.h>

void ShowSockStatus() {
  for(int x = 0; x < MAX_SOCK_NUM; x++) {
    Serial.print(WiFi._state[x]);    
    Serial.print("  ");
    Serial.print(WiFi._server_port[x]);    
    Serial.print("  s=");
    Serial.print(serverDrv.getServerState(x));    
    Serial.print("  c=");
    Serial.print(serverDrv.getClientState(x));    
    Serial.print("  d=");
    Serial.println(serverDrv.availData(x));    

  }
}

SurferTim avatar Dec 17 '13 10:12 SurferTim

Excellent, thanks. I'll add this to my debug printouts. In fact, I am trying to understand why the server stops working after some hours. My server serves a short web page from an Uno with the WiFi shield, then answers an ajax request about once per second. Two servers work at the same time, with the browser showing them in two separate tabs. At the same time, the Arduino reads some sensors and makes computations on them. I use the SchedulerARMAVR by Fabrice Oudert to put both web server on a cooperative task on their own(both on the same task), and have added several yield() calls to the standard libraries. After some hours (typically less than 20 hours) one of the servers stops. Sometimes reloading the page from the browser is enough to get it started again, sometimes not, and I hope your diagnostic code above can help me find out why, or at least how to automatically restart a frozen server.

One note and one pledge to the Arduino developers: I understand that the WiFi shield is obsolete: the Yún costs about the same price as the WiFi shield alone, yet it additionally includes a Leonardo, an Ethernet socket and a second powerful processor in the same space. But people who have bought the WiFi shield should not be abandoned, especially given that the WiFi shield, in one of its versions, is still available on the Arduino shop.

fpoto avatar Dec 17 '13 13:12 fpoto

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

agdl avatar Jul 12 '16 13:07 agdl