C# - C:stop 阻塞子线程上的侦听套接字

C# - C:stop blocking listen socket on a child thread

我的程序:
我创建了 2 threads:Thread 1 和一个监听套接字,线程 2 做一些事情。
但是线程 1 阻塞了程序,我无法启动线程 2,直到线程 1 上的侦听套接字接收到数据。 但是我同时需要2个线程运行,并且不需要在2个线程之间保持同步。(但仍在同一个程序中)。
怎么做??? 我的代码是这样的:

Thread thread1 = new Thread(new ThreadStart(a.thread1));
Thread thread2 = new Thread(new ThreadStart(b.thread2));
try
            {
                thread1.Start();
                thread2.Start();
                thread1.Join();   // Join both threads with no timeout
                                  // Run both until done.
                thread2.Join();
            }

程序停止在线程 1(侦听套接字)。
而且我不想使用非阻塞套接字(我正在使用阻塞套接字)。
监听套接字应该阻塞子线程,但不应该阻塞我的程序。

我最近遇到了同样的问题并解决了这个问题:

    private static void StartServers()
{
    for (int i = Convert.ToInt32(ConfigurationManager.AppSettings.Get("PortStart"));
                    i <= Convert.ToInt32(ConfigurationManager.AppSettings.Get("PortEnd"));
                    i++)
                {
                    var localAddr = IPAddress.Parse(ConfigurationManager.AppSettings.Get("IpAddress"));
                    var server = new TcpListener(localAddr, i);
                    Servers.Add(server);
                    server.Start();
                    StartAccept(server);
                }
}
private static void StartAccept(TcpListener server)
    {
        server.BeginAcceptTcpClient(OnAccept, server);
    }
private static void OnAccept(IAsyncResult res)
    {
        var client = new TcpClient();
        try
        {
            Console.ForegroundColor = Console.ForegroundColor == ConsoleColor.Red
                ? ConsoleColor.Green
                : ConsoleColor.White;
            var server = (TcpListener) res.AsyncState;
            StartAccept(server);
            client = server.EndAcceptTcpClient(res);
            Console.WriteLine("Connected!\n");
            var bytes = new byte[512];
            // Get a stream object for reading and writing
            var stream = client.GetStream();


            int i;

            // Loop to receive all the data sent by the client.
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                // Translate data bytes to a ASCII string.
                var data = Encoding.ASCII.GetString(bytes, 0, i);
                Console.WriteLine("Received: {0} \n", data);

                // Process the data sent by the client.
                data = InterpretMessage(data);
                var msg = Encoding.ASCII.GetBytes(data);

                // Send back a response.
                stream.Write(msg, 0, msg.Length);
                Console.WriteLine("Sent: {0} \n", data);
            }
        }
        catch (Exception exception)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            client.Close();
            Console.WriteLine(exception.Message);
        }
    }

基本上,它所做的是创建一个异步接收系统,您可以在其中让多个服务器同时侦听多个端口。请记住,每个端口只能有一个侦听器。

在您的示例中,您可以简单地调用 StartServers 方法,然后直接继续您的应用程序正在执行的操作。

为了使其适用于单个端口上的单个服务器,只需删除 StartServers 中的循环并配置 1 个 TcpListener 并启动它。