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

发布于 2024-08-06 23:53:30 字数 53 浏览 5 评论 0原文

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

I have a c++ program using winsock2. I would like to know how to tell when someone's connection to my program closes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赏烟花じ飞满天 2024-08-13 23:53:30

使用select等待socket上的读取;当套接字关闭时,winsock 应该将其报告为可读。从套接字接收将返回 0 个字节,告诉您套接字已关闭。

Use select to wait for reading on the socket; when the socket is closed winsock should report it as readable. Receiving from the socket will then give you 0 bytes, telling you that the socket was closed.

一身骄傲 2024-08-13 23:53:30
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
}
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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文