具有多个客户端的TCP服务器

时间:2013-10-07 06:47:25

标签: c# sockets tcp tcpserver

我正在研究TCP服务器/客户端应用程序。

我的问题是:

我的服务器应用程序启动一个新线程并阻止它直到接受连接 listenforClient方法

但是,当多个客户端连接到我的服务器并且他们同时请求不同的东西时,我如何管理连接?我如何管理客户端1获取其需要的信息以及客户端2的相同信息。

它是多线程的,因此多个客户端可以连接,但我如何处理请求。 我不想把所有东西都放在1方法中。

先谢谢

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


        }
    }

1 个答案:

答案 0 :(得分:5)

您的客户端将以不同的线程发送,因此它们不会相交。 您只需添加“DispatchMethod”之类的内容即可处理您的邮件。

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))
{
    ...
}