TCPServer behaving strangely

So i copied the TCPServer example code and tried it. worked just fine.
yet when i make a small change to know when the client connects or disconnects it behaves strangely.
When i first connect, it works fine. But if i do not send anything for about 15 seconds, then i start getting all kinds of strange characters that keep printing endlessly even if i close the client connection.

Here is the original TCPServer code.

TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
    // start listening for clients
    server.begin();

    Serial.begin(9600);

    delay(1000);

    Serial.println(Network.localIP());
    Serial.println(Network.subnetMask());
    Serial.println(Network.gatewayIP());
    Serial.println(Network.SSID());
}

void loop()
{
  if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
      server.write(client.read());
    }
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }
}

My code is here.

void loop()
{

	  if (client.connected())
	  {
		  Serial1.println("Client Connected");

		  while(client.connected())
		  {
			    // echo all available bytes back to the client
		  	  if (client.available() )
			  {
				t = client.read();

				Serial1.print((char)t);
			  }
		  }

		  Serial1.println("Client Closed");
	  }
	  else
	  {
	    // if no client is yet connected, check for a new connection
	    client = server.available();
	  }

}

It looks like you’re outputting anything that’s coming over the socket, which could be raw bytes and not necessarily text. What are you connecting to?

– You’re also looping on “client.connected” which is very different from “client.available”. “available” will tell you when there are things to read, so this is probably the source of the weird behavior.

1 Like

Since its acting as a server on port 23, i am using a telnet client app on my PC to connect to the Spark Core.
My idea is to check for a client connection which is the "if (client.connected())“
then once they connect i want to print that.
Then i want to be in a while loop the whole time the client is connected which is why i am then using the “while(client.connected())” and lastly i only want to print something when there is data. which is the
"if (client.available() )” .

To me it all seems ok, unless i am of course using the a wrong call,
The strange thing is while the client is connected and i am in that while loop. I put a Serial1.println(client.available()); just after the the "if (client.available() )"
when it first starts its ok, then after about 15 seconds it starts printing strange stuff even if i close the client.
What i saw was after the 15 seconds, the print1 results showed 512, 511,510…
So it seems that after a certain time, the buffer count get wrapped back around for some reason.
It just keeps doing that when it gets to 0.

Found something interesting out. see below for detail.
Basically, if your going to use networking, and you put a while(1) in the loop section your going to run into trouble because you need to exit the loop function at some point for it to do housekeeping .
A remedy to this was that i just needed to add this in my while loop.
SPARK_WLAN_Loop();

Now it all works just fine.

If you use their code as shown here
void loop()
{

  if (client.connected())
  {
  	  while(client.available())
	  {
		t = client.read();

		Serial1.print((char)t);
	  }
  }
  else
  {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }

}

And simply wrap that with a while(1) it does not work at all. It’s probably because it’s not allowing to exit the loop() function so it can do other housekeeping. Which is also why my other code was not working as well.

void loop()
{

while(1)
{
	  if (client.connected())
	  {
	  	  while(client.available())
		  {
			t = client.read();

			Serial1.print((char)t);
		  }
	  }
	  else
	  {
	    // if no client is yet connected, check for a new connection
	    client = server.available();
	  }

}

}