Suggestion for improving Ethernet library

It would seem that the developers never added a built-in method for detecting client connect/disconnect - the existing library requires the client to connect and send data before the connection can be detected.

Here’s a modification to the library that I made that seems to work well for me, and hopefully others. Feel free to comment/improve my suggestion:

+Add on line 16:

EthernetClient connected();

+Add on line 51:

EthernetClient EthernetServer::connected()

  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)) {
        // XXX: don't always pick the lowest numbered socket.
        return client;

  return EthernetClient(MAX_SOCK_NUM);


// declare a global variable to flag when the connection or disconnection occurs
bool clientConnected=false;
void setup(){
	// .. do standard ethernet library setup
void loop(){
	// Wait for ethernet client
	EthernetClient client = server.connected();
		// read bytes from incoming client and echo the results
			// Detected a client connect
			client.println("Hello there, dave.");
			DEBUG_PRINTLINE("Client Connected!");

		// read any incoming data
			char c =;
			// print out the incoming character
	}else if(clientConnected){
		Serial.println("Client disconnected!");

Hope this helps someone. 8)

How does that differ from the available() method, in terms of results?

available() only returns with an object if there is data waiting to be read. So simply put, a client could be connected but we would never know it unless it sent us some data to read. This allows you to detect if the client is connected, even if they have not sent you any data - which is important in commandless read-only applications or simply knowing if your client is still connected.

I'm reading everything I can about Ethernet on this forum to try to find something relevant to my own problem and post. Might your improvement relate to my question from yesterday?

In short: WebServerworks (web_HelloWorld) is unreliable for me. Serves a few times to the browser, but occasionally spews garbage consisting of snippets of previous sessions to the browser.

Thanks for any ideas,

I don't think my suggestion will apply in your case - this is more of a concern with the design of the EthernetServer itself as it lacked that necessary function. In a web server scenario, the client will always issue a request on connect so this isn't applicable. But it would appear someone sent you in the right direction in your thread - hope it worked for you!