Tcp socket closed detection
Posted: Thu Jun 20, 2019 11:05 pm
I have created a TCP client over wifi with the ESP and it connects to my TCP Server on my windows pc just fine. I can do data transmissions, send and receive. Though if I close the server connection I don't get any notification nor can I find a way to poll for if the socket is still active. The only details around this was I found within the arduino API.
This is how I send and receive data atm. I started trying out the errno method like with POSIX C, but regardless of state, it is always 11. Whether the server was opened or closed.
This is how I send and receive data atm. I started trying out the errno method like with POSIX C, but regardless of state, it is always 11. Whether the server was opened or closed.
Code: Select all
if (wifiState.wifiConnected && !wifiState.socketConnected) {
// Create the socket for communication to the client
if (connectToSocket(&wifiState, (char*)"10.0.0.7")) {
char str[] = "Hello world\n";
send(wifiState.clientSocket, (void *)str, strlen(str), 0);
wifiState.socketConnected = true;
}
else {
wifiState.socketConnected = false;
puts("wifiState.socketConnected = disconnected");
}
}
if (wifiState.wifiConnected && wifiState.socketConnected) {
char buf[256];
int bytes_received = recv(wifiState.clientSocket, buf, 10, MSG_DONTWAIT);
if (bytes_received > 0) {
buf[bytes_received] = 0; //null safety
printf("socket read: %s\n", buf);
}
else if (bytes_received < 0) {
//error found...socket closed?
printf("return %d. errno %d\n", bytes_received, errno);
// wifiState.socketConnected = false;
}
}