简体   繁体   English

TCP/IP 服务器如何监听多个客户端?

[英]How TCP/IP server listens to several clients?

I'm setting up a small tcp/ip connection using GSM/GPRS modems, I have a server PC which runs a server (listener) program, and have several (more than 100) modems located in different places, they send small packets of data to the server in specific periods.我正在使用 GSM/GPRS 调制解调器建立一个小型 tcp/ip 连接,我有一台运行服务器(监听器)程序的服务器 PC,并且有几个(超过 100 个)调制解调器位于不同的地方,它们发送小数据包在特定时期向服务器发送数据。

I have tested system with one client, but how can I test it with several clients?我已经用一个客户端测试了系统,但是如何用多个客户端测试它? how will my server respond to several clients?我的服务器将如何响应多个客户端?

Here is my server code:这是我的服务器代码:

    private TcpListener tcpListener;
    private Thread listenThread;
    public static TcpClient client;

    public Form1()
    {
        InitializeComponent();
        this.tcpListener = new TcpListener(IPAddress.Any, 2020);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }
    private void ListenForClients()
    {
        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            client = this.tcpListener.AcceptTcpClient();


            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }
    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        //TcpClient client = new TcpClient(servername or ip , port);
        //IpEndPoint ipend = tcpClient.RemoteEndPoint;
        //Console.WriteLine(IPAddress.Parse(ipend.Address.ToString());

        //label3.Text = IPAddress.Parse(((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()).ToString();
        SetControlPropertyThreadSafe(label3, "Text", IPAddress.Parse(((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()).ToString());

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                break;
            }

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();
            //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

            string received_text = encoder.GetString(message, 0, bytesRead).ToString();

            SetControlPropertyThreadSafe(label1, "Text", received_text);

            if (received_text == "cmdtim")
            {
                SendData(DateTime.Now.ToString());
            }
        }

        tcpClient.Close();
    }

As you see I've created a separate thread for listening clients, how can I listen to several clients?如您所见,我为监听客户端创建了一个单独的线程,我如何监听多个客户端?

Should I create a thread for each client?我应该为每个客户创建一个线程吗?
I don't know how many clients will I have in each moment, will the server buffer data of other clients when it listens to a specific client?我不知道我每一刻会有多少个客户端,服务器在监听特定客户端时会缓冲其他客户端的数据吗?

What is the best strategy for listening to several clients and also sending all of them data?监听多个客户并发送所有数据的最佳策略是什么?

Some advise:一些建议:

Remove:消除:

public static TcpClient client;

Replace:代替:

client = this.tcpListener.AcceptTcpClient();

with

TcpClient client = this.tcpListener.AcceptTcpClient();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM