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

标签 c# .net sockets udp client

我有一个可与 Udp 一起使用的套接字程序。

我要管理连接到服务器的客户端。

例如,如果客户端连接到服务器,则将该客户端添加到列表中。
我已经在 Tcp 中使用以下代码完成了此操作:

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

例如,仅列表中的客户,他的消息就会被写入。

我将每个客户端与GetStream()进行了比较。但是在Udp中,我不知道该怎么做。我可以管理客户吗?

编辑

我使用以下代码从客户端接收消息:
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);
}

提前谢谢

最佳答案

此代码对我来说效果很好!
服务器:

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

        }
    }

客户:
    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);
        }
    }

不要忘记客户端和服务器正在通信。因此,他们必须使用折衷的方式进行沟通。如果您想存储客户信息,则该信息必须是恒定的。为此,必须将它bind到固定的localEndPoint
您还可以在问题注释中使用解决方案:您可以在消息中添加ID。您也可以同时使用。
附注:我使用的是Socket类,而不是TCPClientUDPClient,因为我发现它们有些尴尬且不一致。

关于c# - 在C#中连接到Udp服务器的客户端列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657377/

相关文章:

c# - 在 TextBox 控件中创建超链接

c# - 将 CSS 应用于 HTML 通用控件,如 ASP.NET 中的 <UL> 和 <LI>

c# - 如何将 sbyte[] 转换为 base64 字符串?

javascript - 如何在用户发送消息时停止 'is typing'功能

c# - 什么会导致 UDP 套接字上的 ConnectionReset?

c# - 如何在本地/内部托管 Azure 辅助角色?

c# - 从图形调整图像大小?

c# - 如何在Elasticsearch上使用NEST进行的查询中访问脚本字段?

mysql - 工作台显示 : Failed to Connect to MySQL at 127. 0.0.1 :3306 through SSH tunnel at 34. xxx.xxx.xxx:22,用户为 root

c# - 在 Xamarin Android 项目上使用 glTF C# 加载器