Detect remote disconnect (half-close) when using asynchronous sockets in C/C++
<p><strong>Detect remote disconnect (half-close) when using asynchronous sockets in C/C++</strong></p>

<p>I have implemented a TCP/IP client using C sockets running in asynchronous mode (<code>fcntl(sock, F_SETFL, O_ASYNC);</code>) in order to minimise blocking. The client will only send data to the server and never receive any data. Thus the loop looks something like this:</p>

<pre class="lang-cpp prettyprint-override"><code>while (true) {
    ...
    fd_set writefds;
    FD_ZERO(&amp;writefds);
    FD_SET(socketFd, &amp;writefds);
    select(socketFd + 1, NULL, &amp;writefds, NULL, &amp;selectTimeout);
    if (FD_ISSET(socketFd, &amp;writefds)) {
       ssize_t cBytes = send(socketFd, someBuffer, someSize, 0);
       ...
    }
    ...
 }
</code></pre>

<p>How do I detect if the server disconnects? As suggested <a href="https://stackoverflow.com/questions/17705239/is-there-a-way-to-detect-that-tcp-socket-has-been-closed-by-the-remote-peer-wit">in this question</a>, an initially plausible solution is to call <code>recv(socketFd, &amp;buf, len, MSG_PEEK);</code> and check if that call returns 0. However, this clearly does not work because first of all, the server does not actually send any data in my case. Second, in the generalised case the server might not send data all the time and <code>recv()</code> will thus return 0 anyway as it is non-blocking.</p>

<p>When using tcpdump to monitor the connection between the server and the client, the server sends a FIN packet when it closes the connection but the client does not acknowledge this. I have been unable to make it recognise the half-close using neither <code>recv()</code>, <code>send()</code>, nor <code>select()</code>. In fact, the first indication that something is wrong is when the server sends a RST when the client tries to send data to the now closed connection:</p>

<pre><code>IP6 ::1.62179 &gt; ::1.2526: Flags [S], seq ..., length 0
IP6 ::1.2526 &gt; ::1.62179: Flags [S.], seq ..., length 0
IP6 ::1.62179 &gt; ::1.2526: Flags [.], ack 1, ..., length 0
IP6 ::1.2526 &gt; ::1.62179: Flags [.], ack 1, ..., length 0
IP6 ::1.2526 &gt; ::1.62179: Flags [F.], seq 1, ..., length 0
IP6 ::1.62179 &gt; ::1.2526: Flags [.], ack 2, ..., length 0
IP6 ::1.2526 &gt; ::1.62179: Flags [.], ack 1, ..., length 0
IP6 ::1.62179 &gt; ::1.2526: Flags [P.], seq 1:7, ack 2, ..., length 6
IP6 ::1.2526 &gt; ::1.62179: Flags [R], seq ..., length 0
</code></pre>

<p>How do I determine that the server has sent the FIN packet so that I can cleanly shutdown everything before trying to send the next data packet?</p>