简体   繁体   English

我们如何知道StreamSocket的状态?

[英]How do we know a StreamSocket's state?

How do I check if a SteamSocket is connected or not? 如何检查SteamSocket是否已连接?

I have found nothing related or useful in the official docs and slides. 在官方文档和幻灯片中,我没有发现任何相关或有用的内容。

Thanks in advance 提前致谢

after several hours of googling i figured out this is not a problem with Poco . 经过数小时的谷歌搜索,我发现这对Po​​co来说不是问题 it seems by design C sockets themselves dont know about their own states, and Poco sockets are based on C sockets, so everything just makes sense. 似乎在设计上C套接字本身并不了解自己的状态,而Poco套接字基于C套接字,因此一切都说得通。

(i havent learned C/C++ basics, i got in programming with Qt directly, whose sockets do provide functions for checking connection states. due to the lack of basic C/C++ knowledge my conclusion above could be wrong ) (我没有学过C / C ++基础知识,我直接用Qt编程,其套接字确实提供了检查连接状态的功能。 由于缺乏基本的C / C ++知识,我的上述结论可能是错误的

for blocking sockets : by now the only solution i found is to use StreamSocket::receiveBytes(), it returns 0 if the peer had used shutdown() to close the connection, and throws exceptions if connection lost or something alike. 用于阻塞套接字 :到目前为止,我找到的唯一解决方案是使用StreamSocket :: receiveBytes(),如果对等方使用shutdown()关闭连接,则返回0,如果连接丢失或类似原因,则引发异常。

for non blocking sockets: totally no idea :) 对于非阻塞式插座:完全不知道:)

Checking whether a socket is still connected is way harder than it sounds at first. 检查插座是否仍然连接起来比一开始听起来要困难得多。 For example, the socket may still be connected from the view of your system, but the other host may have crashed. 例如,从系统的角度来看,套接字可能仍已连接,但其他主机可能已崩溃。 Or there may be a problem in the network, etc. 或网络中可能存在问题等。

Generally, if you'd just like to know whether you haven't yet called close() on the socket, you can do: 通常,如果您只想知道是否尚未在套接字上调用close(),则可以执行以下操作:

bool connected = socket.impl()->initialized();

On the other hand, if you really need to find out whether a connection is still alive, you'll have to send a message to the peer and wait for its response. 另一方面,如果您确实需要确定连接是否仍然存在,则必须向对等方发送一条消息,并等待其响应。 That's the only reliable way to tell the connection is alive. 这是告诉连接仍然有效的唯一可靠方法。 If you just need to find out whether the peer has closed the socket, try a poll() with short timeout, followed by a receiveBytes() (if poll() has indicated that there's something to read), which will return 0 if the peer has closed the connection. 如果只需要查找对等方是否已关闭套接字,请尝试使用超时时间短的poll() ,然后尝试接收receiveBytes() (如果poll()表示有需要读取的内容),如果对等方已关闭连接。 You can also try setting the socket to nonblocking and calling receiveBytes() . 您也可以尝试将套接字设置为nonblocking并调用receiveBytes()

I also recommend having a look a the Unix Socket FAQ . 我还建议您查看Unix Socket FAQ

When writing network code, whether TCP sockets or HTTP, or any other protocol, I highly recommended having a solid knowledge of the specific protocols and mechanisms, and not just use a high-level API and hope for the best. 在编写网络代码(无论是TCP套接字还是HTTP或任何其他协议)时,我强烈建议您对特定的协议和机制有扎实的知识,而不仅仅是使用高级API并希望达到最佳效果。

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

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