Strange behavior of TCPServer and TCPClient

Update to the TCPServer thread in general. I looked into this in response to a question from new user @Cretcheu.

The problem with the previous example code (which we should still get working) is that the call to TCPServer::available() constructs a new TCPClient when a currently connected client has no bytes available. I have posted new TCPServer example code that works with the current :spark: TCPServer implementation.

The most important change is a slightly different loop that only calls server.available() when needed rather than every time:

if (client.connected()) {
    while (client.available()) {
        server.write(client.read());
    }
} else {
    client = server.available();
}

I can flash this example from the web IDE, connect from my laptop using telnet, and get results echoed exactly as intended.

2 Likes