C# -- TcpListener.Start() 导致 SocketException 并带有消息“每个套接字地址仅使用一次"; [英] C# -- TcpListener.Start() causing SocketException with message "Only one usage of each socket address"

查看:15
本文介绍了C# -- TcpListener.Start() 导致 SocketException 并带有消息“每个套接字地址仅使用一次";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,它将在 TcpListener 实例上调用 Start() 方法.此侦听器正在使用一个不常见的端口,并且不知道任何其他服务都在使用该端口.很少会在一分钟左右的时间内遇到奇怪的错误.一分钟后,服务(在失败时立即重新启动)因以下异常而背靠背崩溃:

I have a service which as it's coming up invokes the Start() method on a TcpListener instance. This listener is using a port that is not common and not known to be used by any other services. Very very rarely for a span of minute or so it experiences an odd error. For a minute the service (which on failure restarts immediately) back to back crashes on the following exception:

   SocketException
   at System.Net.Sockets.Socket.DoBind(System.Net.EndPoint, System.Net.SocketAddress)
   at System.Net.Sockets.Socket.Bind(System.Net.EndPoint)
   at System.Net.Sockets.TcpListener.Start(Int32)
   at MyTestServer.Server.StartListening()

异常信息如下:

Only one usage of each socket address (protocol/network address/port) is normally permitted

这个文章建议我遇到端口耗尽,我应该调整注册表以修改 WinSock 的超时和端口范围值.这一切都很好,但我只有(或预计会有)50-100 个客户端连接.我怎么可能用完端口?机器人和端口扫描器?

This article suggests that I'm experience port exhaustion and that I should tweak the registry to modify timeout and port range values for WinSock. This is all good and well, but I only have (or expect to have) 50-100 clients connecting. How could I possibly run out of ports? Bots and port scanners?

推荐答案

这可能不是那个帖子中的端口耗尽.他们正在使用 WCF(类似于 TCPClient)进行连接.您正在绑定到 TCP 端口并等待连接.严格来说,这不是一回事.您正在绑定到特定的 TCP 端口号.当您收到此消息时,Windows(无论是否正确)认为它正在使用中.Windows 报告另一个进程已经绑定到该端口,所以你不能.您可以有 2 个进程尝试使用该端口并遇到此问题,因此 50-100 的数字不是问题.

It probably isn't port exhaustion as it was in that post. They were connecting out with WCF (which would be like the TCPClient). You are binding to a TCP Port and waiting for the connection. It isn't strictly the same thing. You are binding to a specific TCP Port number. When you get this message, windows (correctly or not) thinks that it is in use. Windows is reporting that another process is already bound to that port, so you can't. You can have 2 processes trying to use the port and get this problem, so the 50-100 figure isn't the issue.

要么是您在其他地方导致了此问题,要么是其他应用程序导致了此问题.

Either you are causing this problem somewhere else or there is some other application that is causing this problem.

如果服务遇到问题并且没有调用 TCPListener.Stop(),然后服务自己重启,它不能绑定到端口,因为windows可能不知道进程已经完成了.

If the service is experiencing a problem and dies without ever calling TCPListener.Stop(), then the service restarts itself, it won't be able to bind to the port because windows may not know the process is done with it.

您必须发布更多详细信息才能进一步了解它.

You'll have to post more details to get into it any further.

这篇关于C# -- TcpListener.Start() 导致 SocketException 并带有消息“每个套接字地址仅使用一次";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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