c ++ winsock2,如何判断连接何时关闭

时间:2009-10-05 21:40:25

标签: c++ sockets winsock

我有一个使用winsock2的c ++程序。我想知道如何判断某人与我的程序的连接何时关闭。

2 个答案:

答案 0 :(得分:5)

使用select等待套接字读取;当套接字关闭时,winsock应该将其报告为可读。然后从套接字接收将给你0字节,告诉你套接字已关闭。

答案 1 :(得分:0)

int received_bytes = recv(_socket, buffer, sizeof(buffer)-1,0);
if(received_bytes > 0)
{
   //data received
}
else if (received_bytes == 0)
{
  //connection closed
}

else
{
  //wait for more data
}