Hello,
I am making a telnet port 23 server on an adafruit grandcentral with wiznet w5500 ethernet shield. I can detect when I initially connect with a putty terminal to the server. The server echoes back characters from the client.
I can't find out how can the arduino detect if the putty terminal client closes the connection.
Or alternatively, how do I detect that a new connection is initiated after a previous one was closed so that I can run again ''telnetClient.println("Hello, telnet client!\n\r ");'' Advice please.
Here's my server code. I won't include the setup() part that works. I run within the loop() the telnetProcess() function.
EthernetServer telnetServer(23);
boolean isTelnetAlreadyConnected = false;
void telnetProcess() {
// wait for a new client:
EthernetClient telnetClient = telnetServer.available();
// when the client sends the first byte, say hello:
if (telnetClient) {
if (telnetClient.connected() ) {
digitalWrite(PIN_LED, true);
}else{
//digitalWrite(PIN_LED, false);//this does not detect that a client disconnected
}
if (!isTelnetAlreadyConnected) {
// clean out the input buffer:
telnetClient.flush();
Serial.print("telnetProcess() We have a new client");
telnetClient.println("Hello, telnet client!\n\r ");
isTelnetAlreadyConnected = true;
}
if (telnetClient.available() > 0) {
// read the bytes incoming from the client:
char thisChar = telnetClient.read();
// echo the bytes back to the client:
telnetServer.write(thisChar);
Serial.print(thisChar);
}
}else{
//digitalWrite(PIN_LED, false); this always runs if the connection is a live, but no characters are available
if (telnetClient.connected() ==false) {
//digitalWrite(PIN_LED, false); this always runs if the connection is a live, but no characters are available
}
}
}