gpt4 book ai didi

java - isConnected 和 isClosed 之间的区别

转载 作者:行者123 更新时间:2023-12-02 03:46:31 25 4
gpt4 key购买 nike

我本以为调用isConnected()套接字上会告诉我是否已连接到另一端。

Returns: true if the socket successfuly connected to a server

但是经过检查然后调用flush()在我得到的套接字上

java.net.SocketException: Broken pipe

怎么样isConnected不同于isClosed ,每个人的真实行为是什么?

如何判断另一端是否正式关闭而不向流写入任何内容或创建新连接?有办法吗?

最佳答案

I would have assumed that calling isConnected() on a socket would tell me whether if is connected to the other side or not.

错了。它告诉您是否曾经连接过此套接字。它不会告诉您连接的状态。

Returns: true if the socket successfuly connected to a server

请注意,它并没有说“当前已连接”到服务器。

How is isConnected different than isClosed, and what is the real behavior of each?

两者的真实行为是它们告诉您套接字做了什么,而不是连接的状态是什么。

How do I tell if the other side if officially closed without writing anything to the streams, or creating new connections? Is there even a way?

不,没有。如果对端正常关闭,则读取将返回 EOS 指示(read() 返回 -1、readLine() 返回 null、readXXX() 对于任何其他 XXX 都会抛出 EOFException)。写入操作将抛出 IOException“连接重置”或“损坏的管道”,具体取决于您的平台。 TCP 不支持“拨号音”性质的任何内容,因此如果没有挂起的写入,则无法查询当前的连接状态。

关于java - isConnected 和 isClosed 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15869495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com