3

I have created a socket using the following lines of code and i get a valid socket and connection is established between the client and server machines. Is there a possibility that the socket becomes invalid due to network disturbances or any other reason.

If so how do we check whether the socket is valid or not.

    SOCKET SocServer; 

    //To Set up the sockaddr structure 
    ServerSock.sin_family = AF_INET; 
    ServerSock.sin_addr.s_addr = INADDR_ANY     
    ServerSock.sin_port = htons(PortNumber);//port number of 5005 

    // To Create a socket for listening on PortNumber 
    if(( SocServer = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) 
    { 
        return FALSE; 
    } 

    //To bind the socket with wPortNumber 
    if(bind(SocServer,(sockaddr*)&ServerSock,sizeof(ServerSock))!=0) 
    { 
        return FALSE; 
    } 

    // To Listen for the connection on wPortNumber 
    if(listen(SocServer,SOMAXCONN)!=0) 
    { 
        return FALSE; 
    }

I know i can check for INVALID_SOCKET which in other words means the socket is 0. Is there any other way out because my SocServer will have a value say 2500, i want to check if this socket is valid.

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
ckv
  • 10,539
  • 20
  • 100
  • 144
  • My comments on sockets: http://stackoverflow.com/questions/2843277/c-winsock-p2p/2920787#2920787 – default Jun 15 '10 at 17:17

4 Answers4

8

Pass your socket to any one of the windows socket functions (eg. getsockopt()), if the socket is invalid, it will return SOCKET_ERROR while WSAGetLastError() will return WSAENOTSOCK.

It is important to note that INVALID_SOCKET does not equal 0(the actual value, which you should not use specifically is ((SOCKET)(~0))

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • Thanks and what is difference between 0 and ((SOCKET)(~0)) – ckv Jun 15 '10 at 15:55
  • `((SOCKET)(~0))` is (literally) `NOT 0`, ie. `0xFFFFFFFF` assuming that `SOCKET` is a 32 bit type. In any case, you should only check the return values of `socket()` and `WSASocket()` against this value. – Hasturkun Jun 15 '10 at 16:43
3

The socket can become "invalid" when either side (expected or unexpected) disconnects, but not out of the blue due to network interferences (unless it disconnects, read above).

You can detect this by checking the return values of send() and recv() for -1 (SOCKET_ERROR)

LukeN
  • 5,590
  • 1
  • 25
  • 33
  • 3
    AFAIK, this is not correct, a socket will remain valid (though not necessarily usable) until its last handle is closed (at which point the handle is invalid, though the socket may live on). – Hasturkun Jun 15 '10 at 15:15
  • That's why I wrote "\"invalid\"" and not "invalid", because I don't think a socket can really be invalid - it can only be connected or not connected, and the latter can be detected by checking the return values! – LukeN Jun 15 '10 at 15:38
2

The socket will not go invalid. But after you listen() you must accept() a connection. That connection may be lost at some point. It could be detected by a failure to write to the socket or recv() returning with error. Then you just recycle back to listen().

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • I do call accept and establish connection with the client. However by some means the connection seems to get disconnected as in TCPView software it does not list any connection for my application. – ckv Jun 15 '10 at 15:57
1

First, assuming that INVALID_SOCKET (which you don't show) is actually defined as 0 this is incorrect. socket returns -1 on failure like the other system calls (0 is a valid fd that could be returned).

Then once you accept a connection, your send/write or recv/read call will return -1. If you're trying to write, errno will be set to EPIPE to indicate the socket was closed (by your client). There's no way to just ask if a socket is closed.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • INVALID_SOCKET is signed -1 only on unix like platforms. On Microsoft windows INVALID_SOCKET is unsigned ~0 (binary NOT 0) and depends on the register size. On a 32 bit MS Windows it is 0xFFFFFFFF and double size on 64 bit platform. – Ingo Mar 25 '23 at 18:06