简体   繁体   中英

TCP server with multiple Clients

I am working on TCP server/client application.

My question is:

My server application starts a new thread and blocks it until connection is accepted the listenforClient method

But how can I manage the connections when multiple Clients are connected to my server and they request different things at the same time how can i manage that client 1 gets info its need and same for client 2.

It's multithreaded, so multiple Clients can connect but how can i process the request. I don't want to put everything in 1 method what than would.

Thanks in Advance

private void serverstart()
    {
        this.tcplistener = new TcpListener(IPAddress.Any, 49151);
        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
            TcpClient client = this.tcplistener.AcceptTcpClient();


            // here was first an message that send hello client
            //
            ///////////////////////////////////////////////////

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

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

            bufferincmessage = encoder.GetString(message, 0, bytesRead);


            if (System.Text.RegularExpressions.Regex.IsMatch(bufferincmessage, Properties.Settings.Default.REQLogin, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                bufferincmessageresult = bufferincmessage.Split('^');
                nickname_Cl = bufferincmessageresult[1];
                password_Cl = bufferincmessageresult[2];
                getuserdata_db();
                login();

                byte[] buffer = encoder.GetBytes(inlogmessage);

                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }


        }
    }

Your client will be dispatched in different threads, so they will not intersect. You just need to add something like "DispatchMethod" where your messages will be processed.

using System.Text.RegularExpressions;
...

if (Regex.IsMatch(bufferincmessage, Properties.Settings.Default.REQLogin, RegexOptions.IgnoreCase))
{
    ...
}
else if (Regex.IsMatch(bufferincmessage, /*some of your command1*/, RegexOptions.IgnoreCase))
{
    ...
}
else if (Regex.IsMatch(bufferincmessage, /*some of your command1*/, RegexOptions.IgnoreCase))
{
    ...
}

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