网络套接字上的 C# BinaryReader.ReadString

发布于 2024-12-08 17:26:57 字数 706 浏览 1 评论 0原文

我有一个服务器/客户端应用程序。

两者在通信时都使用BinaryReader/Writer。

当客户端和服务器快速交换消息时(在给定的一秒内有很多消息),并且我大多数时候(但并非总是)关闭服务器(通过内置命令,有序关闭),客户端的 BinaryReader.ReadString() 方法会抛出EndOfStreamException,这很好。我不明白的是为什么这个异常不将 TcpClient.Connected 属性更改为“false”?

while(true){
   try{
      BinaryReader.ReadString()
   }
   catch(IOException){
     if(!TcpClient.Connected)
        break;
     //BinaryWriter.Write() - this will, eventually, change Connected property to 'false'
   }
}

这将无限循环。我认为 Connected 属性会在网络读/写失败时发生变化。如果 BinaryReader 抛出异常,那么它没有成功读取,是吗?

如果我放入 BinaryWriter.Write(),则无限循环将被打破,因为 Connected 属性更改为“false”。

相关问题,EndOfStreamException 是否始终表明网络连接断开,或者是否意味着临时问题?

I have a server/client app.

Both use BinaryReader/Writer when communicating.

When the client and server are exchanging messages rapidly, many in a given second, and I shutdown the server (via a built in command, orderly shutdown) most of the time (but not always) the client's BinaryReader.ReadString() method throws a EndOfStreamException, which is fine. The thing that I don't understand is why doesn't this exception change the TcpClient.Connected property to 'false'?

while(true){
   try{
      BinaryReader.ReadString()
   }
   catch(IOException){
     if(!TcpClient.Connected)
        break;
     //BinaryWriter.Write() - this will, eventually, change Connected property to 'false'
   }
}

This will loop endlessly. I thought that the Connected property changes on unsuccessful network read/write. If BinaryReader is throwing exceptions then it isn't successfully reading, is it?

If I throw in a BinaryWriter.Write(), then the endless loop is broken because Connected property is changed to 'false'.

Related question, does an EndOfStreamException always indicate a broken network connection or could it mean a temporary problem?

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

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

发布评论

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

评论(1

南风几经秋 2024-12-15 17:26:57

通过设计。来自 TcpClient.Connected 的 MSDN 库文章的备注部分:

因为 Connected 属性只反映了连接的状态
从最近一次操作开始的连接,您应该尝试发送
或接收消息以确定当前状态。留言后
发送失败,该属性不再返回 true。请注意,这
行为是设计使然的。您无法可靠地测试该状态
连接,因为在测试和发送/接收之间的时间内,
连接可能已丢失。您的代码应该假设
套接字已连接,并优雅地处理失败的传输

您发现的解决方法是正确的。

By design. From the Remarks section of the MSDN Library article for TcpClient.Connected:

Because the Connected property only reflects the state of the
connection as of the most recent operation, you should attempt to send
or receive a message to determine the current state. After the message
send fails, this property no longer returns true. Note that this
behavior is by design. You cannot reliably test the state of the
connection because, in the time between the test and a send/receive,
the connection could have been lost. Your code should assume the
socket is connected, and gracefully handle failed transmissions

The workaround you discovered is the correct one.

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