Is it possible to have data in a receive buffer after a socket is closed?
For example, if a client connects to the W5500, sends data, and immediately closes the socket (so the socket goes to SOCK_CLOSED instead of CLOSE_WAIT and the client didn’t send a FIN packet). If I didn’t read the data before the socket is closed, can I still read the data?
Therefore, to make sure I don’t miss any data in a server, do I need to check RX_RSR > 0 even for SOCK_CLOSED connections?
I would say it differently - your application should consider for data to come while socket is in established state (we talk about TCP), but when socket’s state is no more “established”, you must check receive buffer again once more to see if there’s some data still left you should get from there. As soon as you got remaining data from the RX buffer, you are free to reuse the socket. There’s no need to continuously check closed sockets for new data because closed sockets do not receive new data.
Thanks for the reply! Let me ask a different question…
Would this code work OK?
uint socketStatus = w5500.readSnSR(sock);
if (socketStatus != SnSR::LISTEN)
{
// 1) this might read RX_RS on a closed socket but thats OK, right?
// 2) a closed socket might have data in the buffer that still needs to be read
if (w5500.getRXReceivedSize(sock) > 0)
ProcessData(sock);
}
if (socketStatus == SnSR::CLOSED)
RecreateSocket(sock); // change the socket back to LISTEN
The idea is that a socket can be established, close_wait, closed, or any other status and still have data in the receive buffer. The only time I know a receive buffer is empty is when I re-create (LISTEN) on the socket.
Would the above code work OK in a loop or is that incorrect?
My application is a TCP server that receives small commands and executes them. If a client connects, sends a command, and then disconnects I still want to process the command which is why I care about data even on closed sockets.