Server closes connection after client shuts down the sending side of its socket · Issue #2569 · esp8266/Arduino (original) (raw)
Basic Infos
Hardware
Hardware: ESP-12F on NodeMCU clone (Geekcreit Doit)
Core Version: 2.3.0
Description
A WiFiClient instance (or the underlying TCP/IP lib) obtained from WiFiServer::available() seems to close the TCP connection when it receives a FIN packet. According to the TCP spec, it should go to the CLOSE_WAIT state, which would still allow data to be sent in the opposite direction.
This happens if a client connects to a server running on the ESP, sends some data and shuts down the writing side of the socket before receiving a response. The client socket's reading side is still open in this case, and the server should be able to send a response to the client.
This issue also occurs with ESP8266WebServer, because it uses WiFiServer/Client.
Settings in IDE
Module: NodeMCU 1.0 (ESP-12E Module)
Flash Size: 4MB (3M SPIFFS)
CPU Frequency: 80Mhz
Upload Using: SERIAL
Sketch
#include "ESP8266WiFi.h"
#define WIFI_SSID "..." #define WIFI_PASS "..."
WiFiServer server(1400);
void setup() { Serial.begin(115200); delay(10);
WiFi.begin(WIFI_SSID, WIFI_PASS); Serial.print("Connecting to WiFi."); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println("OK");
server.begin(); }
void loop() { WiFiClient client = server.available(); if (client) { Serial.printf("status before delay: %d\n", client.status()); delay(1000); client.write("Hello, World!\n"); Serial.printf("status after delay: %d\n", client.status()); client.stop(); Serial.printf("status after stop: %d\n", client.status()); } }
Debug Messages
Client that shuts down the sending side immediately
Shell command:
sleep 0 | nc 192.168.178.42 1400
(sending side is shut down immediately, because input pipe of netcat is closed after sleep 0
)
Client output:
Serial output:
status before delay: 4
status after delay: 0
status after stop: 0
(4 is ESTABLISHED, 0 is CLOSED)
Client that waits for the server to close the connection (works correctly)
Shell command:
sleep 2 | nc 192.168.178.42 1400
(this gives the server enough time to respond and close the connection)
Client output:
Serial output:
status before delay: 4
status after delay: 4
status after stop: 0
(4 is ESTABLISHED, 0 is CLOSED)