This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to detect that the peer closed a TCP socket

Hello,

I am using the NCS v1.5.0, the nRF9160 SoC and the modem has firmware version mfw_nrf9160_1.2.3

Based on the UDP sample, I created an application that has opens a TCP listening server then waits for a client to connect. I am using blocking sockets and poll() to wait for events on the server and client sockets. My application is here tcp_listen.zip

It all works as expected, except that my application is not informed when the remote peer has closed the its end of the client socket.

Tracing with Wireshark on the remote peer side, I can see that when it closes the socket it sends a FIN+ACK packet. To which the nRF9160 modem sends an ACK. Nothing more happens. On the application, I expected that when the remote peer closes the client socket, the call to poll() would received a POLLIN, POLLHUP or POLLERR event. But nothing happens.

How can my application be informed that the remote peer has closed its end of the client socket ? 

Best regards,

  Nelson 

Parents
  • Hello Nelson,

    Yes, this should work exactly as you expect. Once peer closes the connection, poll() should return POLLHUP event and the consecutive recv() call should return 0.

    I have to check this with out modem team. Due to summer vacation period most of our experts are out of office currently, but I will come back to you as soon as possible.

    Regards,

    Markus

Reply
  • Hello Nelson,

    Yes, this should work exactly as you expect. Once peer closes the connection, poll() should return POLLHUP event and the consecutive recv() call should return 0.

    I have to check this with out modem team. Due to summer vacation period most of our experts are out of office currently, but I will come back to you as soon as possible.

    Regards,

    Markus

Children
  • Hello Markus,

    Any updates on this issue ? I tried other alternatives (e.g. reading from the socket and closing it case or error), but this also does not work. After I close the socket, recv() returns -1 but errno is always zero.

    [update 28/07/2021] The SO_KEEPALIVE socket option is apparently not available, and using getsockopt() to get the state/error of the client socket also does not detect that the socket was closed. 

    And sending a custom ping to test the connection is not possible, the protocol does not support it.

    Best regards,

     Nelson Gonçalves

Related