在客户端断开连接时检测到

时间:2018-10-03 19:40:29

标签: c++ tcp connection winsock2 disconnect

unsigned _stdcall TcpServ::ClientSession(void *data) {
SOCKET ClientSocket = (SOCKET)data;
fd_set read_s, error_s;
FD_ZERO(&read_s); 
FD_ZERO(&error_s);
FD_SET(ClientSocket, &read_s);
FD_SET(ClientSocket, &error_s);
timeval mine;
mine.tv_sec = 2;
mine.tv_usec = 0;
char recvbuf[128];
Time();
std::cout <<"Client Connected\n";
for (;; Sleep(20)) {
    if ((recv(ClientSocket, recvbuf, sizeof(recvbuf), 0) == 0)||(select(ClientSocket, &read_s, NULL, NULL, &mine)==0)) {
        Time();
        std::cout << "Client disconnected\n";
        break;
    }
    std::cout << recvbuf;
}
return 0;

为什么当客户端发送消息时连接关闭而客户端断开连接,我希望在发送消息时不关闭连接。当客户端呼叫功能关闭或异常断开连接(例如丢失的互联网连接)时,连接必须关闭。如果您非常了解winsock,请在oreder中向我发送联系人信息,以向您提问并更好地学习英语

0 个答案:

没有答案