WIZnet Developer Forum

Can receive buffer (RX_RSR > 0) have data after SOCK_CLOSED?

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.

hello, sofakng

If you do CLOSE command, the socket closed immediately.

But if you do DISCONNECT command, the socket wait for peer’ FIN.

It means if peer send data packet, w5500 can receive it until peer send FIN.

I am hard to understand what you want to do.

But I think It is helpful.

Thank you


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)

  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.

Are there any examples of a multi-user (ie. multiple socket) TCP or Telnet server? Not the Arduino samples but an official Wiznet sample?

I’ve found other examples like a DNS client, DHCP client, and HTTP server, but nothing like a persistent Telnet server.

hello, sofakng

First, the code you show above, I think it would work. But I am not sure caz, it is hard to check all thing.

And I did not know you did not get the w5500 application code.


This link has loopback, dhcp, dnc application codes.

Thank you


Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.