C#客户端通过UPD进行通信

本文关键字:通信 UPD 客户端 | 更新日期: 2023-09-27 18:19:33

我正在寻求一些有关服务器应用程序和客户端之间通信的帮助。这个想法是,我的客户端将监听UDP数据包,读取它,然后根据它读取的内容执行命令。

我的问题是服务器发送数据包,而客户端什么也不做。以下是我的代码片段:

客户:

public void listen()
{
        try
        {
            MessageBox.Show("");
            UdpClient receivingUdpClient = new UdpClient(11000);

            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 11000);
            try
            {
                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                string returnData = Encoding.ASCII.GetString(receiveBytes);
                string[] split = returnData.Split(':');
                if (split[0] == "SayHello")
                {
                  MessageBox.show("Hello user","Hello"); 
                }
                //Note i have many commands but i shortened it to save room. 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
}

服务器:

else if (radioButton4.Checked)
{
        UdpClient udpClient = new UdpClient([IP_ADDRESS_HERE], 11000);
            body = richTextBox1.Text;
            title = textBox1.Text;
            Command = "Message" + ":" + body + ":" + title + ":" + 4;
            Byte[] sendBytes = Encoding.ASCII.GetBytes(Command);
            try
            {
                udpClient.Send(sendBytes, sendBytes.Length);
            }
            catch (Exception)
            {
                Console.WriteLine(e.ToString());
            }
        }

只是想看看你们能不能找到我忽略的东西。

C#客户端通过UPD进行通信

检查您的Windows防火墙并验证它是否阻止您的客户端打开端口11000。控制面板->系统和安全->Windows防火墙