• WiFi
  • How to notice a TCP connection closed by client on server side?
2020/09/23 10:38:07
Hello guys!
Please explain me is it possible to notice on server's side that client closed connection if no calls to "recv" function were done?  My use case is the following (it's quite artificial, but I hope it will help to understand my concern). I have a TCP server running on uC + WINC1500. It conducts the following steps when starting up:
  1. socketTcp = socket( AF_INET, SOCK_STREAM, 0 );
  2. bind( socketTcp, (struct sockaddr*) &socketAddress, sizeof(struct sockaddr_in) );
  3. listen( socketTCP, 0 );
  4. waitForClientConnectionOrTimeout();
  5. waitForClientDisconnectionOrTimeout();
  6. close( socketTcp );
At step 3 I'm waiting for incoming connection from the client and if it does then just reports about it and waits until client disconnects to report that as well.
The issue here is that I did not find a way to capture "disconnection" event at step 5. The only option I have according to datasheet is to call between steps 4 and 5 the "recv" function. In this case the dropped connection will be reported in socket callback function as "SOCK_ERR_CONN_ABORTED" message (returned as negative value of "s16BufferSize" field).
But at the same time API documentation claims the following:
"The application received the required data in response to this asynchronous call through the reception of the event SOCKET_MSG_RECV in the socket callback.
Receiving the SOCKET_MSG_RECV message in the callback with zero or negative buffer length denotes the following:
SOCK_ERR_NO_ERROR : Socket connection closed. The application now call close()
SOCK_ERR_CONN_ABORTED : Socket connection aborted. The application should now call close()
SOCK_ERR_TIMEOUT : Socket receive timed out. The socket connection remains open."
As you see, it's claimed that under some conditions SOCK_ERR_NO_ERROR fires, and I thought it's a good sign of a dropped connection, but it's unclear when it happens, because if "recv" function was called, then socket closed at client's size triggers SOCK_ERR_CONN_ABORTED.
So please tell me when does aforementioned SOCK_ERR_NO_ERROR occurs and is it mandatory to call "recv" function immediately after connection acceptance even to track status of that connection?
Thanks in advance!
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account