简体   繁体   English

检测套接字连接何时断开 C / C++ Unix

[英]Detect when Socket Connection is disconnected C / C++ Unix

I know this question has been discussed several times, but I didn't manage to detect whether a socket disconnected in C / C++ on Unix.我知道这个问题已经讨论过好几次了,但我没能检测到 Unix 上的 C / C++ 中的套接字是否断开。 Is there any way to do so?有什么办法吗?

Thx, Regards from Brooks谢谢,布鲁克斯的问候

I assume that the socket might be closed by the peer.我假设套接字可能被对等方关闭。

It very depends on where you want to detect the disconnection.这很大程度上取决于您要检测断开连接的位置。

If you are in the blocking recv : manpage如果您处于阻塞recv联机帮助页

These calls return the number of bytes received, or -1 if an error occurred.这些调用返回接收到的字节数,如果发生错误,则返回 -1。 In the event of an error, errno is set to indicate the error.如果发生错误,则设置 errno 以指示错误。

When a stream socket peer has performed an orderly shutdown, the return value will be 0 (the traditional "end-of-file" return).当 stream 套接字对等体执行了有序关闭时,返回值为 0(传统的“文件结束”返回)。

If you want to know on send : manpage如果您想在send时知道:联机帮助页

On success, these calls return the number of bytes sent.成功时,这些调用返回发送的字节数。 On error, -1 is returned, and errno is set appropriately.出错时,返回 -1,并适当设置 errno。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM