WiFi Client - TCP Communication

PikaCZ
Posts: 1
Joined: Sat Oct 29, 2022 5:13 pm

WiFi Client - TCP Communication

Postby PikaCZ » 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
  1. [812835][D][WiFiClient.cpp:545] connected(): Disconnected: RES: 0, ERR: 128
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
  1.  
  2. .....
  3.  
  4. //Connecting to server
  5. while(!RemoteClient.connect(server_host, server_port))
  6. {
  7.     Serial.println("Waiting beafore connect");
  8.     delay(2500);
  9.     ledState = !ledState;
  10.     digitalWrite(LED_GREEN_PIN, ledState);
  11.     Serial.print("Connecting to ");
  12.     Serial.println(server_host);
  13. }
  14. ......
  15.  
  16. void loop() {
  17.  
  18.  
  19. if (RemoteClient.connected()) //<---- Here the program stops for 100s after the server breaks communication
  20. {
  21. ....
  22. }
  23. else
  24. {
  25.   sleep(100);
  26.   Serial.println("No connction with server. Reconnect");
  27.   Serial.print(".");
  28.   RemoteClient.stop();
  29.   ConnectToServer();
  30.  }
  31.  
  32. }
  33.  
Bellow log from serial monitor
  1.  
  2. 18:27:35.591 -> [812835][D][WiFiClient.cpp:545] connected(): Disconnected: RES: 0, ERR: 128  
  3. 18:29:15.593 -> No connction with server. Reconnect
  4. 18:29:15.593 -> .Connecting to server                                                                  
  5. 18:29:38.255 -> [935514][D][WiFiClient.cpp:545] connected(): Disconnected: RES: 0, ERR: 128  
  6. 18:31:18.272 -> No connction with server. Reconnect
  7. 18:31:18.272 -> .Connecting to server                                                                

Who is online

Users browsing this forum: No registered users and 82 guests