Ethernet Shield as Server, Multiple Connections?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am

Ethernet Shield as Server, Multiple Connections?

Post by stephanie »

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:

Code: Select all

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:

Code: Select all

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!

User avatar
q2dg
 
Posts: 8
Joined: Mon Aug 24, 2015 10:56 am

Re: Ethernet Shield as Server, Multiple Connections?

Post by q2dg »


Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”