How can I check if a client disconnected through Winsock in C++?

后端 未结 4 1363
抹茶落季
抹茶落季 2020-12-15 23:06

How can I check if a client disconnected through Winsock in C++?

4条回答
  •  臣服心动
    2020-12-15 23:32

    Beej's Network Programming Guide

    if you call recv in blocking mode and it returns with 0 bytes read, the socket has disconnected, else it wait for bytes to be received.

    Look in this FAQ 2.12

    example from select on this page.

    int nRet;
    
    if(( nRet = select( 0, &fdread, NULL, NULL, NULL )) == SOCKET_ERROR )
    {
        // Error condition
        // Check WSAGetLastError
    }
    
    if( nRet > 0 )
    {
        // select() will return value 1 because i m using only one socket
        // At this point, it should be checked whether the
        // socket is part of a set.
    
        if( FD_ISSET( s, &fdread ))
        {
            // A read event has occurred on socket s
        }
    }
    

提交回复
热议问题