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:
#EthernetServer.h
+Add on line 16:
EthernetClient connected();
#ethernetServer.cpp
+Add on line 51:
EthernetClient EthernetServer::connected()
{
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)) {
// XXX: don't always pick the lowest numbered socket.
return client;
}
}
return EthernetClient(MAX_SOCK_NUM);
}
Usage:
// 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();
if(client){
// read bytes from incoming client and echo the results
if(!clientConnected){
// Detected a client connect
clientConnected=true;
client.flush();
client.println("Hello there, dave.");
DEBUG_PRINTLINE("Client Connected!");
}
// read any incoming data
if(client.available()){
char c = client.read();
// print out the incoming character
Serial.println(c);
}
}else if(clientConnected){
clientConnected=false;
Serial.println("Client disconnected!");
}
}
Hope this helps someone. 8)