Here is my attempt at a TCP-server, which should answer incoming telnet-connections to port 88 and just output an increasing counter, disregarding any input. My counter gets to around 252, and after that the esp8266 does still accept and terminate the connections, but does not output the text or number anymore. So to me this problem seems to be that I am terminating the connection wrongly, that some data gets accumulated until all 256 bytes of (something) are used up. NB: if I wait several minutes (by terminating the telnet-loop and later restarting it, while doing nothing to the esp8266) the counting sometimes continues with count-values > 1000, and sometimes the esp8266 has completely restarted with count 0 again...
So the question is, how to properly terminate a connection, completely erasing any leftover data from the previous one?
The way I'm testing this is by using a telnet-loop from my raspberry, giving the following output:
pi@raspberrypi ~ $ while true; do telnet 192.168.2.112 88 ; done
...
Trying 192.168.2.112...
Connected to 192.168.2.112.
Escape character is '^]'.
Count: 250
Connection closed by foreign host.
Trying 192.168.2.112...
Connected to 192.168.2.112.
Escape character is '^]'.
Count: 251
Connection closed by foreign host.
Trying 192.168.2.112...
Connected to 192.168.2.112.
Escape character is '^]'.
Count: 252
Connection closed by foreign host.
Trying 192.168.2.112...
Connected to 192.168.2.112.
Escape character is '^]'.
Connection closed by foreign host.
Trying 192.168.2.112...
Connected to 192.168.2.112.
Escape character is '^]'.
Connection closed by foreign host.
...
And the testcode:
#include <ESP8266WiFi.h>
WiFiServer server(88);
long count;
void setup()
{
count=0;
WiFi.mode(WIFI_STA);
WiFi.begin("xxx", "yyy");
while (WiFi.status() != WL_CONNECTED)
{
delay(200);
}
server.begin();
}
void loop()
{
WiFiClient client = server.available();
if (client)
{
if (client.connected())
{
count++;
client.print("Count: ");
client.println(count);
client.stop();
}
}
}
(I have also tried flushing any incoming data before client.stop() in the code above, by trying
while(client.available())
{
client.read();
}
delay(50);
client.flush();
and similar... Same result.)