TCP 套接字错误 10061 [英] TCP socket error 10061

查看:173
本文介绍了TCP 套接字错误 10061的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Windows 服务套接字程序来监听特定端口并接受客户端请求.它工作正常.

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.

因此我增强了应用程序以绑定到端口 0(零),以便它可以接受任何可用端口上的请求.

So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.

但后来我收到错误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.

但是客户端需要发送连接到特定端口的请求.例如,client1 应该连接到 port 8030,client2 应该连接到 port 8031.

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.

推荐答案

因此我增强了应用程序以绑定到端口 0(零),以便它可以接受任何可用端口上的请求.

So I enhanced the application to bind to port 0(zero), so that it can accept the request on any available port.

错了.0 表示操作系统应该分配一个端口.一台服务器一次只能监听一个端口.监听套接字只接受新连接.

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.

新连接将具有相同的本地端口,但使用了 IP 标头中源 (ip/port) 和目标 (ip/port) 的组合识别连接.这就是为什么同一个监听套接字可以接受多个客户端.

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 获得了对广播的支持,如果这正是您所需要的.

UDP got support for broadcasts if that's what you are looking for.

更新:

一个非常简单的例子

  Socket client1 = serverSocket.Accept(); // blocks until one connects
  Socket client2 = serverSocket.Accept(); // same here

  var buffer = Encoding.ASCII.GetBytes("HEllo world!");
  client1.Send(buffer, 0, buffer.Count); //sending to client 1
  client2.Send(buffer, 0, buffer.Count); //sending to client 2

只需为您要接受的每个客户不断调用 Accept.我通常使用异步方法(Begin/EndXXX)来避免阻塞.

Simply keep calling Accept for each client you want to accept. I usually use the asynchronous methods (Begin/EndXXX) to avoid blocking.

这篇关于TCP 套接字错误 10061的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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