簡體   English   中英

如何處理TCP C#套接字服務器中的多個活動連接?

[英]How to handle multiple active connections in a TCP C# socket server?

到目前為止,我的套接字服務器非常簡單:

        public static void listen()
    {
        TcpListener server = null;
        IPAddress address = IPAddress.Parse("127.0.0.1");
        try
        {
            server = TcpListener.Create(5683);
            server.Start();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace);
        }


        while (true)
        {
            Thread.Sleep(10);
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("Accepted Client");

            Thread thread = new Thread (new ParameterizedThreadStart(SwordsServer.ClientHandler));
            thread.IsBackground = true;
            thread.Start(client);
        }
    }

    public static void ClientHandler(object c)
    {
        TcpClient client = (TcpClient)c;
        NetworkStream netstream = client.GetStream();
        bool connected = true;
        while (connected)
        {
            Thread.Sleep(10);
            try
            {
                byte[] bytes = new byte[client.ReceiveBufferSize];                   
                netstream.Read(bytes, 0, bytes.Length);
                Console.WriteLine("got data");
                netstream.Write(bytes, 0, bytes.Length);

            }
            catch (Exception e)
            {
                connected = false;
                Console.WriteLine(e);
                Console.WriteLine(e.StackTrace);
            }
        }
    }

我的問題是,從概念上講,您將如何在每個唯一client上保持標簽,並將更新從其他線程發送到特定客戶端?

例如,如果我有特定客戶的數據,我該如何聯系該客戶而不是廣播它? 或者,如果客戶端不再連接,我該怎么辦?

在此先感謝您的協助!

您用於接受多個連接的實現將創建匿名客戶端,這意味着在連接超過1個之后,您將無法識別正確的客戶端。 如果確定是問題所在,那么您可以做一件事,讓客戶端將標識符發送給服務器,例如“ Client1”。 創建一個Dictionary,然后在您的方法ClientHandler()中,從客戶端讀取標識符,並將TCPClient的對象添加到字典中。

因此,修改后的代碼將如下所示:

 Dictionary<string, TCPClient> dictionary = new Dictionary<string, TCPClient>();


 public static void ClientHandler(object c)
    {
        TcpClient client = (TcpClient)c;
        NetworkStream netstream = client.GetStream();
        bool connected = true;
        while (connected)
        {
            Thread.Sleep(10);
            try
            {
                byte[] bytes = new byte[client.ReceiveBufferSize];

                //read the identifier from client
                netstream.Read(bytes, 0, bytes.Length);

                String id = System.Text.Encoding.UTF8.GetString(bytes);

                //add the entry in the dictionary
                dictionary.Add(id, client);
                Console.WriteLine("got data");
                netstream.Write(bytes, 0, bytes.Length);

            }
            catch (Exception e)
            {
                connected = false;
                Console.WriteLine(e);
                Console.WriteLine(e.StackTrace);
            }
        }
    }

請注意:您的應用程序應該足夠智能,可以動態地決定將更新發送給哪個客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM