Java 套接字断开连接报告与 C# 断开连接

发布于 2024-08-31 20:17:08 字数 236 浏览 5 评论 0原文

在 C# 中,当套接字连接终止时,另一个节点会在终止链接之前收到通知,因此剩余节点可以更新连接状态。

在Java中,当我终止通信链接时,另一个节点不断报告连接有效。

我是否需要实现一个读取周期(有意义),当它在读取期间收到 -1 时将连接报告为丢失(在 C# 中,我认为这是 0)?

感谢您的见解。

编辑:谢谢你们俩。正如我怀疑并在我的帖子中提到的那样,需要进行额外的检查来确认连接的连接状态。

in C# when a sockets connection is terminated the other node is informed of this before terminating the link thus the remaning node can update the connection status.

in Java when i terminate a communication link the other node keeps reporting the connection as valid.

do i need to implement a read cycle (makes sense) that reports the connection as lost when it recieves a -1 during read (in C# this is 0 i think)?

thank you for your insight.

EDIT: thanks to you both. as i suspected and mentioned in my post that an additional check is required to confirm the connected state of a connection.

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

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

发布评论

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

评论(2

极致的悲 2024-09-07 20:17:08

如果连接的远程端断开,并且可以检测到断开连接,通常您会从 InputStream/InputChannel 收到 IOException。如果它无法检测到断开连接,那么当套接字读取超时时,最终将抛出 IOException。等待超时的时间长度可以使用 Socket.setSoTimeout() 进行调整。

If the remote side of the connection goes away, normally you'll get an IOException from the InputStream/InputChannel if the disconnection can be detected. If it can't detect the disconnect an IOException will eventually be thrown when the socket read times out. The length of time it waits for a timeout can be adjusted using Socket.setSoTimeout().

只涨不跌 2024-09-07 20:17:08

在java中,只有当您从套接字读取/写入或查询输入流状态(例如InputStream.available())时,您才会发现套接字的另一端被关闭。我认为没有任何异步通知另一端已关闭。

您如何测试套接字是否仍然打开?

您可以轮询 InputStream.available() 方法,如果返回 -1,则表明套接字已关闭。当然,如果适合您的用途,您也可以读取数据。

请参阅 InputStream.available()

In java, you find out about the other end of the socket being closed only when you read/write to/from the socket, or query the input stream state (e.g. InputStream.available()). I don't think there is any asynchronous notification that the other end is closed.

How are you testing that the socket is still open?

You can poll the InputStream.available() method and if that returns -1, you know the socket is closed. Of course, you can also read data, if that fits with your usage.

See InputStream.available()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文