TCP 套接字错误 10061

发布于 2025-01-01 03:35:02 字数 1150 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

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

发布评论

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

评论(1

或十年 2025-01-08 03:35:02

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

错误的! 0 表示操作系统应分配一个端口。服务器一次只能侦听一个端口。监听套接字只接受新连接。

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

如果您正在寻找的话,UDP 获得了对广播的支持。

更新:

一个非常简化的示例:

  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)来避免阻塞。

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

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:

  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

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

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