Ethernet shield - determine # of 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 - determine # of connections?

Post by stephanie »

I'm trying to figure out if this is possible... if so, it's not obvious how.

I've got the ethernet shield with the wiznet module, and using the standard arduino ethernet library. The arduino is configured as a server, and this is working correctly. According to documentation, the arduino can handle up to 4 connections. In my testing, I've tried two concurrent connections and this works fine.

What I want, however, is to be able to know how many clients are connected, if any.

The Server.available() only returns a valid client object if there is data waiting to be processed; if a client is connected but idle then Server.available returns false.

I've looked through the ethernet libarary sources and the only thing that I've found that looks promising is EthernetClass::_state[4] which I think might return a 'state' value for each of the 4 possible connections... I don't know how to call / access this from my sketch though.

Any suggestions / advise would be most welcome!

Cheers!

stephanie
 
Posts: 295
Joined: Sat Dec 11, 2010 1:17 am

Re: Ethernet shield - determine # of connections?

Post by stephanie »

Well after some experimentation, I figured out how to do it -- and incase anyone else ever needs to know, here's how.

The way it works is not using the _state value, but the EthernetClass::_server_port value instead. The ethernet library only assigns a port to one of its 4 sockets, to keep the number of active sockets at (number of clients) plus one. Here is a code snippet:

Code: Select all

int myPort = 80; // the port number your arduino is listening on

int i;
int clients = -1;
for(i=0; i<4; i++) {
  if(EthernetClass::_server_port[i] == myPort) clients++;
}
Serial.print("The number of connected ethernet clients is ");
Serial.println(clients, DEC);
As there are only 4 sockets available, once a third client connects this code can't tell if the fourth socket is active or just listening, but for my purposes I was more interested in being able to tell if anyone was connected rather than exact numbers. So it's going to return 3 even if a fourth client connects.

I've tested this with the current version of the Arduino IDE and it is working.

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

Return to “Arduino Shields from Adafruit”