如何修复此 SocketException

发布于 2024-07-15 23:06:43 字数 951 浏览 5 评论 0原文

我有一个循环,它打开一个套接字,作用于该套接字,然后关闭该套接字并重新启动。 但是,在第二次迭代中,我得到 SocketException,通常只允许每个套接字地址(协议/网络地址/端口)使用一次。
但是,套接字应该关闭,netstat -a 不会显示我正在侦听该端口或任何其他内容。 引发异常的代码是:

_bindedLocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_bindedLocalSocket.Bind(new IPEndPoint(Util.ChannelProfileToListeningAddress(_profile), _profile.ListenPort));
_bindedLocalSocket.Listen(30);
_bindedLocalSocket.BeginAccept(new AsyncCallback(OnRequested), null);

但是,我认为罪魁祸首不是该代码,就在我到达该代码之前,在我尝试关闭连接之前,我得到了这个:

An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at Poderosa.PortForwarding.SynchronizedSocket.EndReceive(IAsyncResult ar)
   at Poderosa.PortForwarding.Channel.OnSocketData(IAsyncResult result)

一旦我关闭程序并再次运行它,它可以使第一个连接正常,第二个连接的行为完全相同(SocketException)。 有人知道我该如何解决这个问题吗?

I have a loop that opens a socket, acts on the socket, then closes the socket and restarts.
However, on the second iteration I get SocketException, Only one usage of each socket address (protocol/network address/port) is normally permitted.
However, the socket should be closed, netstat -a doesn't show that I'm listening on that port or anything. The code that throws the exception is:

_bindedLocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_bindedLocalSocket.Bind(new IPEndPoint(Util.ChannelProfileToListeningAddress(_profile), _profile.ListenPort));
_bindedLocalSocket.Listen(30);
_bindedLocalSocket.BeginAccept(new AsyncCallback(OnRequested), null);

However, I think the culprit is not that code, just before I get to that code, and before I try to close the connection I get this:

An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at Poderosa.PortForwarding.SynchronizedSocket.EndReceive(IAsyncResult ar)
   at Poderosa.PortForwarding.Channel.OnSocketData(IAsyncResult result)

Once I close the program and run it again, it can make the first connection fine, the second one acts just the same (SocketException).
Does anybody have any idea how I can fix this?

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

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

发布评论

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

评论(2

吃颗糖壮壮胆 2024-07-22 23:06:43

我认为 socket.Close() 可以解决这个问题,但我想不是。 在这种情况下,您需要使用:

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

您可以阅读有关套接字选项的更多信息 这里

I thought that socket.Close() took care of that, but I guess not. In that case, you need to use:

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

You can read more about socket options here.

寻找一个思念的角度 2024-07-22 23:06:43

这是 Windows 上的一个已知问题,即使您关闭了套接字,在资源释放回操作系统之前也会有一段等待时间。

有一种方法可以在 .NET 中手动设置套接字的等待时间,但是重新审视您的设计可能是更好的选择,保持侦听器套接字打开,而不是关闭它,而是关闭新的套接字在 Socket.Accept() 期间创建;

It's a known issue on windows, that even if you've closed a socket, there is a waittime before the resource is released back to the OS.

There is a way to manually set the waittime for a socket in .NET, however it may be a better option to revisit your design, to instead keep the listener socket open, instead of closing it down, and instead close the new socket that is created during Socket.Accept();

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