How to detect a socket disconnect?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

HI,

How can I detect a socket disconnection?

I have a socket connected to a machine somewhere, if the remote machine closes the socket, how can I detect that event?

I tried:

wiced_tcp_register_callbacks

But the callbacks are never called. Is there a way to say something like:

If(SocketIsConnected(socket)) {

     //do something

}

Thanks

0 Likes
1 Reply
Félix_T
Level 5
Level 5
10 sign-ins First comment on blog 50 replies posted

There are quite a few ways of doing this especially if we take "socket disconnect" as a lose term.

The first check you should always do is, were you able to send the data?  If not, the socket is likely disconnected (a couple of retries are recommended for this).

The second check to do is usually to have a keep alive packet that you send to the server/client and have it send it back to you.  This ensures that communication is flowing correctly.  If this times out, the socket is likely disconnected.

We have found that trying to detect a disconnect purely by receiving on the socket doesn't yeild much.  It's always OK to timeout on a socket receive, especially if there is nothing to receive.  Usually we will see erros on an attempt to send, depending on the error we can mostly deduce that the socket was disconnected.

Here is our send code (slightly modified, because some stuff outside of the context of our system makes no sense)

while (err <= 0 && retryCount < 4)

      {

         err = lwip_send(MonSocket, pkt, length, MSG_MORE);

         if (err < 0)

         {

            retryCount ++;

            if (errno == EAGAIN || errno == EINPROGRESS)

            {

               Logger_Warn("Transmit trying again\n");

               if (retryCount >= 4)

               {

                  Logger_Error("giving up on packet\n");

                  consecutivePacketDrops++;

                  if (consecutivePacketDrops > 2)

                  {

                     Logger_Error("giving up on connection\n");

                     CHANGED CODE: TELL SYSTEM SOCKET IS GONE

                  }

                  break;

               }

               continue;

            }

            else

            {

               snprintf(msg, 40, "Message not transmitted: %d\n", errno);

               Logger_Event(msg);

              CHANGED CODE: TELL SYSTEM SOCKET IS GONE

               break;

            }

         }

         else if (err < length)

         {

            Logger_Event("Incomplete packet transmitted\n");

         }

         else

         {

            // Packet successfully transmitted

            consecutivePacketDrops = 0;

         }

      }

An errno of 104 means that the other side (client or host) actively sent a FIN or a FIN/ACK to terminate the connection.  If your send reports this, the socket is definitely gone.

Obvisouly we are using LWIP directly, but the errnos are standard accross the system.

0 Likes