Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-C:停止阻塞子线程上的侦听套接字_C#_Multithreading_Sockets_Asyncsocket - Fatal编程技术网

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

C#-C:停止阻塞子线程上的侦听套接字,c#,multithreading,sockets,asyncsocket,C#,Multithreading,Sockets,Asyncsocket,我的程序:我创建了两个线程:线程1带有侦听套接字,线程2执行一些操作。 但线程1阻塞了程序,在线程1上的侦听套接字接收到数据之前,我无法启动线程2。 但我需要两个线程同时运行,并且不需要在两个线程之间保持同步。(但仍在同一程序中)。 怎么做??? 我的代码如下: private static void StartServers() { for (int i = Convert.ToInt32(ConfigurationManager.AppSettings.Get("PortSt

我的程序:
我创建了两个线程:线程1带有侦听套接字,线程2执行一些操作。
但线程1阻塞了程序,在线程1上的侦听套接字接收到数据之前,我无法启动线程2。 但我需要两个线程同时运行,并且不需要在两个线程之间保持同步。(但仍在同一程序中)。
怎么做??? 我的代码如下:

    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);
        }
    }
thread1=新线程(新线程开始(a.thread1));
螺纹2=新螺纹(新螺纹起点(b.thread2));
尝试
{
thread1.Start();
thread2.Start();
thread1.Join();//不超时地连接两个线程
//运行两个直到完成。
螺纹2.连接();

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

    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);
        }
    }
private static void startServer()
{
for(int i=Convert.ToInt32(ConfigurationManager.AppSettings.Get(“PortStart”));

我不能为同步套接字使用非阻塞线程吗?(仅等待停止1进程,而不是程序)你现在的问题不就是你的线程在侦听器接收到数据之前一直处于阻塞状态吗?一个同步操作会阻塞线程,因此也会阻塞连接的线程。也许我应该问一个更好的问题,如果线程正在做独立的工作,为什么线程会被连接?通过连接两个线程,你不是吗可能正在创建争用条件?您是否正在尝试执行我看到的操作:当使用Thread1.join()时,程序将被阻止,直到Thread1(侦听套接字)完成。但如果我不将线程1连接到程序,我可以得到线程的结果吗??(线程1将为我更改一些值)假设类值是静态的,或者您的类是单例的,那么您应该能够。请记住,您不是线程安全的,如果阻塞策略错误,您可能会遇到争用条件。从链接中的示例可以看出,程序将等待线程2完成,本质上是任何更改线程1对共享数据所做的修改应该对线程2可用。