C#-TcpListener.Start()导致SocketException,并显示消息“每个套接字地址只有一种用法". [英] C# -- TcpListener.Start() causing SocketException with message "Only one usage of each socket address"

查看:135
本文介绍了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天全站免登陆