TCP 套接字错误 10061
我创建了一个 Windows 服务套接字程序来侦听特定端口并接受客户端请求。效果很好。
protected override void OnStart(string[] args)
{
//Lisetns only on port 8030
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8030);
//Defines the kind of socket we want :TCP
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Bind the socket to the local end point(associate the socket to localendpoint)
serverSocket.Bind(ipEndPoint);
//listen for incoming connection attempt
// Start listening, only allow 10 connection to queue at the same time
serverSocket.Listen(10);
Socket handler = serverSocket.Accept();
}
但我需要服务程序侦听多个端口并接受任何可用端口上的客户端请求。
因此,我增强了应用程序以绑定到端口 0(零),以便它可以接受任何可用端口上的请求。
但后来我收到错误 10061
No connection could be made because the target machine actively refused it.
我无法知道出现此错误的原因是什么。
任何人都可以建议增强代码以接受任何端口上的请求的方法。
但客户端需要发送请求来连接到特定端口。例如,client1 应连接到端口 8030
,client2 应连接到端口 8031
。
I have created a windows service socket programme to lisen on specific port and accept the client request. It works fine.
protected override void OnStart(string[] args)
{
//Lisetns only on port 8030
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 8030);
//Defines the kind of socket we want :TCP
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Bind the socket to the local end point(associate the socket to localendpoint)
serverSocket.Bind(ipEndPoint);
//listen for incoming connection attempt
// Start listening, only allow 10 connection to queue at the same time
serverSocket.Listen(10);
Socket handler = serverSocket.Accept();
}
But I need the service programme to listen on multiple port and accept the client request on any available port.
So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.
But then I got the error 10061
No connection could be made because the target machine actively refused it.
I am unable to know whats the reason of getting this error.
Can anybody please suggest the way to enhance the code to accept the request on any port.
But the client need to send request to connect to specific port. e.g client1 should connect to port 8030
, client2 should connect to port 8031
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误的! 0 表示操作系统应分配一个端口。服务器一次只能侦听一个端口。监听套接字只接受新连接。
新连接将具有相同的本地端口,但 IP 标头中的源(ip/端口)和目标(ip/端口)的组合用于识别连接。这就是为什么同一个监听套接字可以接受多个客户端。
如果您正在寻找的话,UDP 获得了对广播的支持。
更新:
一个非常简化的示例:
只需为您想要接受的每个客户端继续调用
Accept
即可。我通常使用异步方法(Begin/EndXXX)来避免阻塞。Wrong! 0 means that the OS should assign a port. A server can only listen at one port at a time. The listen socket just accepts new connections.
The new connection will have the same local port, but the combination of source (ip/port) and destination (ip/port) in the IP header is used to identify the connection. That's why the same listen socket can accept multiple clients.
UDP got support for broadcasts, if that's what you are looking for.
Update:
A very simplified example:
Simply keep calling
Accept
for each client you want to accept. I usually use the asynchronous methods (Begin/EndXXX) to avoid blocking.