简体   繁体   English

C#中连接到Udp服务器的客户端列表

[英]List of clients connected to Udp Server in C#

I have a socket program that works with Udp .我有一个适用于Udp的套接字程序。

I want to manage clients that connect to the server.我想管理连接到服务器的客户端。

For example, if the clients is connected to the server, add that client to a list.例如,如果客户端连接到服务器,则将该客户端添加到列表中。 I have already done this in Tcp with the following code:我已经使用以下代码在Tcp 中完成了此操作:

static readonly Dictionary <int, TcpClient> list_clients = new Dictionary <int, TcpClient>();

For example, just the client who is on the list, his messages will be written.例如,只有在列表中的客户,他的消息才会被写入。

I compared each clients with GetStream() .我将每个客户端与GetStream() But in Udp I do not know how to do it.但是在 Udp 中我不知道该怎么做。 Can I manage clients ?我可以管理客户吗?

EDIT编辑

I use the following code to receive a message from the client :我使用以下代码从客户端接收消息:

private static UdpClient udpServer = new UdpClient(11000);
private static IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 11000);

static void Main(string[] args)
{
    var firstData = udpServer.Receive(ref remoteEP);
    Console.WriteLine(Encoding.ASCII.GetString(firtsData);
}

Thanks in advance提前致谢

this code works well for me!这段代码很适合我!
Server:服务器:

    public partial class Server : Form
    {
        public Server()
        {
            InitializeComponent();
        }

        void Print(string message)
        {
            listBox1.Items.Add(message);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(new IPEndPoint(IPAddress.Loopback, 55555));
            byte[] vs = new byte[100];
            EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0);

            Dictionary<EndPoint, int> pairs = new Dictionary<EndPoint, int>();
            int id = 0;
            for (int ii = 0; ii < 5; ii++)
            {
                socket.ReceiveFrom(vs, ref senderRemote);
                if (pairs.ContainsKey(senderRemote))
                    Print(pairs[senderRemote].ToString() + " says:");
                else
                {
                    pairs.Add(senderRemote, id++);
                    Print("new sender");
                }
                Print(senderRemote.ToString());
                Print(Encoding.Unicode.GetString(vs));
                socket.SendTo(vs, senderRemote);
            }

        }
    }

client:客户:

    public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();
        }
        Socket socket;
        private void button1_Click(object sender, EventArgs e)
        {
            socket.Send(Encoding.Unicode.GetBytes("Gholamam!"));
            byte[] vs = new byte[100];
            socket.Receive(vs);
            listBox1.Items.Add(Encoding.Unicode.GetString(vs));
        }

        private void Client_Load(object sender, EventArgs e)
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(new IPEndPoint(IPAddress.Loopback, new Random().Next(1024,65000)));
            socket.Connect(IPAddress.Loopback, 55555);
        }
    }

don't forget that client and server are communicating.不要忘记客户端和服务器正在通信。 therefore they must use compromised way to communicate.因此他们必须使用妥协的方式进行通信。 if you like to store client information, it must be constant.如果你喜欢存储客户信息,它必须是恒定的。 for this, you must bind it to a fixed localEndPoint .为此,您必须bindbind到固定的localEndPoint
you can also use the solution in the question comments: you can add an id in messages.您还可以使用问题评论中的解决方案:您可以在消息中添加一个 id。 also you can use both.你也可以同时使用。
PS: I'm using Socket Class instead of TCPClient or UDPClient because i found them awkward and inconsistent in some manners. PS:我用Socket类来代替TCPClientUDPClient因为我发现他们尴尬和不一致的一些方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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