判断客户端是否断开

BOOL CMySocket::IsDropped()
{
 BOOL bDropped = FALSE;
 INT iRet = 0;
 BOOL bOK = TRUE;
 
 struct timeval timeout = { 0, 0 };//立刻返回,不阻塞
 fd_set readSocketSet;
 
 FD_ZERO( &readSocketSet );
 FD_SET( m_hSocket, &readSocketSet );
 
 iRet = ::select( 0, &readSocketSet, NULL, NULL, &timeout );
 bOK = ( iRet > 0 );
 
 if( bOK )
 {
  bOK = FD_ISSET( m_hSocket, &readSocketSet );
 }
 
 if( bOK )
 {
  CHAR szBuffer[1] = "";
  iRet = ::recv( m_hSocket, szBuffer, 1, MSG_PEEK );
  bOK = ( iRet > 0 );
  if( !bOK )
  {
   INT iError = ::WSAGetLastError();
   bDropped = ( ( iError == WSAENETRESET ) ||
    ( iError == WSAECONNABORTED ) ||
    ( iError == WSAECONNRESET ) ||
    ( iError == WSAEINVAL ) ||
    ( iRet == 0 ) ); //Graceful disconnect from other side.
  }
 }
 
    return( bDropped );
}

你可能感兴趣的:(网络)