简体   繁体   中英

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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