WiFi Client - TCP Communication
Posted: Thu Nov 24, 2022 6:38 pm
Hi,
I am using WiFiClient to connect my ESP32 to a TCP server on my computer. Everything works nice until the server (PC) closes the connection.
When the server closes the connection, the function that returns the connection status waits exactly 100s before returning the connection status
When debugging in the terminal, you can see exactly when the server closed the connection. In terminal I see line like below
How to make the function WiFiClient.connected() return the status immediately ? At the same time when the error message is written out ?
My code below
Bellow log from serial monitor
I am using WiFiClient to connect my ESP32 to a TCP server on my computer. Everything works nice until the server (PC) closes the connection.
When the server closes the connection, the function that returns the connection status waits exactly 100s before returning the connection status
When debugging in the terminal, you can see exactly when the server closed the connection. In terminal I see line like below
Code: Untitled.txt Select all
[812835][D][WiFiClient.cpp:545] connected(): Disconnected: RES: 0, ERR: 128
My code below
Code: Untitled.c Select all
.....
//Connecting to server
while(!RemoteClient.connect(server_host, server_port))
{
Serial.println("Waiting beafore connect");
delay(2500);
ledState = !ledState;
digitalWrite(LED_GREEN_PIN, ledState);
Serial.print("Connecting to ");
Serial.println(server_host);
}
......
void loop() {
if (RemoteClient.connected()) //<---- Here the program stops for 100s after the server breaks communication
{
....
}
else
{
sleep(100);
Serial.println("No connction with server. Reconnect");
Serial.print(".");
RemoteClient.stop();
ConnectToServer();
}
}
Code: Untitled.txt Select all
18:27:35.591 -> [812835][D][WiFiClient.cpp:545] connected(): Disconnected: RES: 0, ERR: 128
18:29:15.593 -> No connction with server. Reconnect
18:29:15.593 -> .Connecting to server
18:29:38.255 -> [935514][D][WiFiClient.cpp:545] connected(): Disconnected: RES: 0, ERR: 128
18:31:18.272 -> No connction with server. Reconnect
18:31:18.272 -> .Connecting to server