Ethernet Shield as Server, Multiple Connections?

According to the info on the Arduino Ethernet shield, it can handle up to 4 connections.

I’m using the shield on a Mega, running the ethernet in server mode. The client connects and sends text to the arduino and receives text back, and remains connected until the client disconnects.

This all works fine, as long as only one client is connected. If a second client connects, the Arduino becomes nonresponsive to the other client. It seems to cache a bit of activity from the ‘frozen’ client, which is then acted on when the other client disconnects.

Looking through the documentation and the library sources, I think the problem lies in the fact that there doesn’t seem to be a way to get a client object for more than one connection. You use server.available() which

Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope

However, in getting ‘a’ client, it doesn’t seem to have provision to get more than one client, or rather, to get additional clients should they exist.

The source for server.available() looks like it just counts up through the four available sockets and returns the first one that has a connection:

EthernetClient EthernetServer::available() {
  accept();
  for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
    EthernetClient client(sock);
    if (EthernetClass::_server_port[sock] == _port &&
        (client.status() == SnSR::ESTABLISHED ||
         client.status() == SnSR::CLOSE_WAIT)) {
      if (client.available()) {
        // XXX: don't always pick the lowest numbered socket.
        return client;
      }
    }
  }
  return EthernetClient(MAX_SOCK_NUM);
}

Despite the comment to the contrary, I don’t see anything in there that indicates that it would not always pick the lowest numbered socket.

So it doesn’t look like it would help if I tried calling server.available four times over, assigning each call to a different client… so this would probably just return four instances of the very same connection:

EthernetClient client1 = server.available();
EthernetClient client2 = server.available();
EthernetClient client3 = server.available();
EthernetClient client4 = server.available();

Does anyone know how to go about handling multiple concurrent connections? Or do I need to do some rewriting of the ethernet library?

Thanks!

What make ethernet shield are you using, what server code are you using, and what are your test methods for making four concurrent connections to the arduino?

I'm using the shield on a Mega, running the ethernet in server mode. The client connects and sends text to the arduino and receives text back, and remains connected until the client disconnects.

This may be the root of your problem. The client makes a request. The server processes the request, returns a response, and hangs up (closes the connection). If you are expecting the server to keep the connection open, you need to rethink that.