如何设置的T​​cpListener总是倾听并接受多个连接? [英] How to set up TcpListener to always listen and accept multiple connections?

查看:276
本文介绍了如何设置的T​​cpListener总是倾听并接受多个连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的服务器应用程序:

Here's my server app:

public static void Main()
{
    try
    {
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

        Console.WriteLine("Starting TCP listener...");

        TcpListener listener = new TcpListener(ipAddress, 500);

        listener.Start();

        while (true)
        {
            Console.WriteLine("Server is listening on " + listener.LocalEndpoint);

            Console.WriteLine("Waiting for a connection...");

            Socket client = listener.AcceptSocket();

            Console.WriteLine("Connection accepted.");

            Console.WriteLine("Reading data...");

            byte[] data = new byte[100];
            int size = client.Receive(data);
            Console.WriteLine("Recieved data: ");
            for (int i = 0; i < size; i++)
                Console.Write(Convert.ToChar(data[i]));

            Console.WriteLine();

            client.Close();
        }

        listener.Stop();
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.StackTrace);
        Console.ReadLine();
    }
}

这是什么样子,它已经是侦听,而跑步,但我还是问这个来指定,我想这两个始终倾听和多个连接的支持。

From what it looks like, it's already always listening while running, but I'm still asking that to specify that I'd like both always-listening and multiple connection support.

我怎么能修改此,同时也在不断的听接受多个连接?

How can I modify this to constantly listen while also accepting multiple connections?

推荐答案

在其上侦听传入的连接通常被称为的监听套接字插座的。
当侦听套接字确认传入连接,通常被称为一个的子插座的创建有效地代表远程端点的插座。

The socket on which you listen for incoming connections is commonly referred to as the listening socket. When the listening socket acknowledges an incoming connection, a socket commonly referred to as a child socket is created that effectively represents the remote endpoint.

为了同时处理多个客户机连接,您将需要产生一个新的线程为每个孩子插座的服务器将接收并处理数据。这样做将允许监听套接字接受并处理多个连接作为您正在收听将不再阻塞或等待,而你等待进入数据的线程。

In order to handle multiple client connections simultaneously, you will need to spawn a new thread for each child socket on which the server will receive and handle data. Doing so will allow for the listening socket to accept and handle multiple connections as the thread on which you are listening will no longer be blocking or waiting while you wait for incoming data.

while (true)
{
    Socket client = listener.AcceptSocket();
    Console.WriteLine("Connection accepted.");

    var childSocketThread = new Thread(() =>
    {
        byte[] data = new byte[100];
        int size = client.Receive(data);
        Console.WriteLine("Recieved data: ");
        for (int i = 0; i < size; i++)
            Console.Write(Convert.ToChar(data[i]));

        Console.WriteLine();

        client.Close();
    });
    childSocketThread.Start();
}

这篇关于如何设置的T​​cpListener总是倾听并接受多个连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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