简体   繁体   中英

List of clients connected to Udp Server in C#

I have a socket program that works with 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:

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() . But in Udp I do not know how to do it. 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 .
you can also use the solution in the question comments: you can add an id in messages. 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.

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