目前,我有两台客户端计算机将Kinect联合数据发送到服务器计算机,但有两个单独的线程在运行侦听器,因此(theString)变量的两个单独副本正在读取数据。有没有办法设置两个不同的变量,从两台不同的客户端计算机读取数据,而不是创建一个重复的线程。

public class AsynchIOServer
{
    static void Listeners(object state)
    {
        TcpListener listener = state as TcpListener;
        using (Socket socketForClient = listener.AcceptSocket())
        {
            if (socketForClient.Connected)
            {
                Console.WriteLine("Client:" + socketForClient.RemoteEndPoint + " now connected to server.");
                using (NetworkStream networkStream = new NetworkStream(socketForClient))
                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream))
                using (System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream))
                {
                    try
                    {
                        while (true)
                        {
                            string theString = streamReader.ReadLine();
                            if (string.IsNullOrEmpty(theString) == false)
                            {
                                Console.WriteLine("Message recieved by  " + theString);
                                var result = theString.Split(',');
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.Message);
                    }
                }
            }
        }
        Console.WriteLine("Press any key to exit from server program");
        Console.ReadKey();
    }

    public static void Main()
    {

        TcpListener[] listeners =
        {
            new TcpListener(15), new TcpListener(10)
        };


        Console.WriteLine("***********This is Server program***********");
        Console.WriteLine("How many clients are going to connect to this server?:");
        int numberOfClientsYouNeedToConnect =int.Parse( Console.ReadLine());


        foreach (TcpListener listener in listeners)
        {
            listener.Start();
            for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
            {
                Thread newThread = new Thread(new ParameterizedThreadStart(Listeners));
                newThread.Start(listener);
            }
        }

    }
}

最佳答案

使用以下类似于状态对象的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace ConsoleApplication1
{
    public class AsynchIOServer
    {
        public class State
        {
            public TcpListener listener { get; set; }
            public StreamReader streamReader { get; set; }
            public StreamWriter streamWriter { get; set; }
            public NetworkStream networkStream { get; set; }
            public Socket socketForClient { get; set; }
        }
        static void Listeners(object obj)
        {
            State state = obj as State;
            using (state.socketForClient = state.listener.AcceptSocket())
            {
                if (state.socketForClient.Connected)
                {
                    Console.WriteLine("Client:" + state.socketForClient.RemoteEndPoint + " now connected to server.");
                    using (state.networkStream = new NetworkStream(state.socketForClient))
                    using (state.streamWriter = new System.IO.StreamWriter(state.networkStream))
                    using (state.streamReader = new System.IO.StreamReader(state.networkStream))
                    {
                        try
                        {
                            while (true)
                            {
                                string theString = state.streamReader.ReadLine();
                                if (string.IsNullOrEmpty(theString) == false)
                                {
                                    Console.WriteLine("Message recieved by  " + theString);
                                    var result = theString.Split(',');
                                }
                            }

                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error: " + ex.Message);
                        }
                    }
                }
            }
            Console.WriteLine("Press any key to exit from server program");
            Console.ReadKey();
        }

        public static void Main()
        {
            State[] states = new State[] {
                new State() { listener = new TcpListener(15)},
                new State() { listener = new TcpListener(10)}
            };



            Console.WriteLine("***********This is Server program***********");
            Console.WriteLine("How many clients are going to connect to this server?:");
            int numberOfClientsYouNeedToConnect = int.Parse(Console.ReadLine());


            foreach (State state in states)
            {
                state.listener.Start();
                for (int i = 0; i < numberOfClientsYouNeedToConnect; i++)
                {
                    Thread newThread = new Thread(new ParameterizedThreadStart(Listeners));
                    newThread.Start(state);
                }
            }

        }
    }

}

关于c# - 如何防止两个线程副本重复方法和变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51847372/

10-16 17:00