判断客户端是否断开

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 );

}

支持原创技术分享,据说打赏我的人,都找到了女朋友!