Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check is a socket is still open?

Tags:

c++

sockets

I have a C++ app that uses standard socket calls and I want to know if I can tell if a socket is still open without sending or receiving any data. Is there a reliable select or ioctlsocket call I can make?

like image 655
Rob Avatar asked May 12 '09 07:05

Rob


1 Answers

If you try to recieve one byte, you can receieve several errors, if you were to have a non-blocking socket, and try to receieve on a valid connection, you will get the error WSAEWOULDBLOCK.

Knowing this we can check a non blocking socket like so

bool connected(SOCKET sock)
{
     char buf;
     int err = recv(sock, &buf, 1, MSG_PEEK);
     if(err == SOCKET_ERROR)
     {
          if(WSAGetLastError() != WSAEWOULDBLOCK)
          {return false;}
     }
     return true;
}

as you can see from the return value of recv recv may return a timeout or several other errors for disconnect, i belive WSAEWOULDBLOCK is the only value it may return if there was an error but still connected, but you may want to double check that list of return values. Also the flag used in recv (MSG_PEEK) means that the data is still read-able when you go to look later after the check, so you don't need to worry about losing one byte of data.

I believe this will only work well with non-blocking sockets, as it may block until it receives data. If you want to use blocking socket you may want to set it non-block with ioctlsocket before this check, then return it to how it was.

like image 65
Owen Delahoy Avatar answered Nov 02 '22 14:11

Owen Delahoy