如何处理 TCP C# 套接字服务器中的多个活动连接? [英] How to handle multiple active connections in a TCP C# socket server?

查看:33
本文介绍了如何处理 TCP C# 套接字服务器中的多个活动连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我的套接字服务器非常简单:

My socket server is pretty simple so far:

        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 并将更新从其他线程发送到特定客户端?

My question is, on a conceptual level, how would you keep tabs on each unique client and send updates from other threads to specific clients?

例如,如果我有特定客户端的数据,我该如何联系该客户端而不是广播它?或者,如果客户端不再连接,我怎么知道?

For example, if I have data for a specific client, how do I reach out to that client instead of broadcasting it? Or, if a client is no longer connected, how could I tell?

提前感谢您的帮助!

推荐答案

您接受多个连接的实现会创建匿名客户端,这意味着在超过 1 个连接后,您将无法识别正确的客户端.如果识别是问题,那么您可以做一件事,让客户端向服务器发送一个标识符,如Client1".创建一个字典,并在您的方法 ClientHandler() 中,从客户端读取标识符并将 TCPClient 的对象添加到字典中.

Your implementation for accepting multiple connections creates anonymous clients, meaning after more than 1 connection you wont be able to identify the right client. If identifying is the problem then you can do one thing, have the client send an identifier to the server like "Client1". Create a Dictionary and in your method ClientHandler(), read the identifier from client and add the TCPClient's object in the dictionary.

所以修改后的代码应该是这样的:

So the modified code would be something like this:

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

请注意:您的应用程序应该足够智能,可以动态决定将更新发送到哪个客户端.

Do note: Your application should be intelligent enough to dynamically decide on to which client the updates should be sent.

这篇关于如何处理 TCP C# 套接字服务器中的多个活动连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆