Photon simple telnet to serial fails

Please try 0.4.5 - we added some code to detect when the remote end is closed.

I am also still having issues with TCPClient.connected() on 0.4.7 returning
true when the client closed the connection.

I am monitoring both client.connected() and client.available() and both are returning true.

I am using the TCPClient.connected() on 0.4.9 on a Photon almost without problems. I have a TCP server and it reliably discovers a client closing the connection. In that case TCPClient.connected() returns false. So that should work fine, now. @Garrett, can you double check that?

However there seems to be one loop hole left: When I have up to 4 parallel connections everything is great. When the client opens a 5th parallel connection then the following happens: The breathing cyan LED flashes a couple of times very quickly. Then the client sees a closed connection. I guess that’s fine, some mechanism to avoid overload with more than 4 connections. I am very glad that’s in place, so it is not too easy to do a denial of service to the Photon. BUT: In that case the TCPClient.connected() call on that connections still returns true. In my case that’s fine, because I can detect the situation when the next write fails. But if there is no write, then the connection seems to stay open forever. @mdma, can you check that, please?

If necessary I could provide some example code, but I think it is really easy to reproduce that.

Thanks in advance!

I tried running the code and the Photon connected but it didn’t print the Photon’s ip address.

    // serial connection
int serialBaud = 9600;

// socket parameters
int serverPort = 23;

// start TCP servers
TCPServer server = TCPServer(serverPort);
TCPClient client;

char myIpString[24];

enum tnetState {DISCONNECTED, CONNECTED};
int telnetState = DISCONNECTED;

    
void setup() {
	Serial.begin(serialBaud); // open serial communications
	server.begin(); // begin listening for TCP connections

    IPAddress myIP = WiFi.localIP();
sprintf(myIpString, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]);
Spark.variable("devIP", myIpString, STRING);
}

void loop() {
	
	if (client.connected()) {
		telnetState = CONNECTED;
		// echo all available bytes back to the client
		int incomingByte = 0;
		while (client.available()) {	//Read incoming TCP data if available and copy to Serial port
			incomingByte = client.read();
			Serial.write(char(incomingByte));
		}
		if (incomingByte != 0)			//Make sure to flush outgoing serial data before looking at serial input
			Serial.flush();

    	while (Serial.available() > 0) { 	//Read incoming serial data if available and copy to TCP port
		incomingByte = Serial.read();
		client.write((char)incomingByte); 	// write the char data to the client
	}
}
else {
	// if no client is yet connected, check for a new connection
	if (telnetState == CONNECTED) {		//If client WAS connected before, so stop() to close connection
		Serial.println("client.stop");	//Check for new connection on next loop()
		client.stop();
		telnetState = DISCONNECTED;
	}
	client = server.available();		//Update TCP client status - "client" is declared globally
}
}

Can you see the IP in the Spark.variable() (which would nowadays be writen Particle.variable("devIP", myIpString);)

This might be some race condition, that the IP hasn’t made it from the WiFi module to the system variabel by the time you request it.
Try adding one or two lines of Particle.process() before you request the IP.

Or you just are too late opening your serial terminal to catch the output.