检测关闭的网络连接 [英] Detect closed network connection

查看:27
本文介绍了检测关闭的网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了许多通过 TCP 进行通信的小程序.我在系统挂起时遇到了无穷无尽的问题,因为一个程序已关闭其网络连接,而另一个端点不知何故未能注意到它现在已断开连接.

I've written a number of small programs that communicate via TCP. I'm having endless issues with the system hanging because one program has closed its network connection, and the other end-point somehow fails to notice that it's now disconnected.

我期待在已关闭的 TCP 连接上执行 I/O 以抛出某种 I/O 异常,但程序似乎只是挂起,永远等待另一端-点回复.显然,如果连接关闭,则永远不会收到回复.(如果您将其放置 20 分钟,它甚至似乎都不会超时.)

I was expecting doing I/O on a TCP connection that has been closed to throw some kind of I/O exception, but instead the program seems to just hang, waiting forever for the other end-point to reply. Obviously if the connection is closed, that reply is never coming. (It doesn't even seem to time out if you leave it for, say, twenty minutes.)

有什么方法可以强制远程端看到"我已经关闭了网络连接?

Is there some way I can force the remote end to "see" that I've closed the network connection?

更新:这是一些代码...

public sealed class Client
{
  public void Connect(IPAddress target)
  {
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    socket.Connect(ipAddress, 1177);
    _stream = new NetworkStream(socket);
  }

  public void Disconnect()
  {
    _stream.Close();
  }
}

public sealed class Server
{
  public void Listen()
  {
    var listener = new TcpListener(IPAddress.Any, 1177);
    listener.Start();
    var socket = listener.AcceptSocket();
    _stream = new NetworkStream(socket);
    ...
  }

  public void Disconnect()
  {
    socket.Shutdown(SocketShutdown.Both);
    socket.Disconnect(false);
  }
}

推荐答案

当应用程序以正确的方式关闭套接字时,它会发送一条包含 0 个字节的消息.在某些情况下,您可能会收到一个 SocketException 指示出现问题.在第三种情况下,在双方之间没有任何通信的情况下,远程方不再连接(例如通过拔掉网线).

When an application closes a socket the right way, it sends a message containing 0 bytes. In some cases you may get a SocketException indicating something went wrong. In a third situation, the remote party is no longer connected (for instance by unplugging the network cable) without any communication between the two parties.

如果最后一件事发生了,您必须将数据写入套接字以检测您无法再访问远程方.这就是发明保活机制的原因——它们经常检查它们是否仍然可以与另一方通信.

If that last thing happens, you'll have to write data to the socket in order to detect that you can no longer reach the remote party. This is why keep-alive mechanisms were invented - they check every so often whether they can still communicate with the other side.

查看您现在发布的代码:当使用 NetworkStream 时,对它的 Read 操作将返回值 0(字节)以指示客户端已关闭连接.

Seeing the code you posted now: when using NetworkStream the Read operation on it would return a value of 0 (bytes) to indicate that the client has closed the connection.

提到了 文档两者

如果没有可供读取的数据,Read 方法返回 0."

"If no data is available for reading, the Read method returns 0."

如果远程主机关闭了连接,并且已经接收到所有可用数据,Read 方法会立即完成并返回零字节."

"If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes."

在同一段落中.实际上,如果在连接打开时没有数据可供读取,NetworkStream 就会阻塞.

in the same paragraph. In reality NetworkStream blocks if no data is available for reading while the connection is open.

这篇关于检测关闭的网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆