代码之家  ›  专栏  ›  技术社区  ›  Niranjan Godbole

无法在c中使用udp发送数据#

  •  2
  • Niranjan Godbole  · 技术社区  · 7 年前

    您好,我正在开发示例应用程序,用于演示udp客户端将数据从客户端发送到服务器。我已经创建了控制台应用程序,下面是我的代码。

    class Program
        {
            static void Main(string[] args)
            {
                senddata();
                while (true)
                {
                    try { 
                    UdpClient udpClient = new UdpClient(9999);
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                    string returnData = Encoding.ASCII.GetString(receiveBytes);
                    string result;
                    result = returnData.ToString();
                    }
                    catch(Exception e)
                    {
    
                    }
                }
                void senddata()
                {
                    UdpClient udpClient = new UdpClient(9999);
    
                    udpClient.Connect("10.170.84.163", 9999);
                    Byte[] senddata1 = Encoding.ASCII.GetBytes("Hello World");
                    udpClient.Send(senddata1, senddata1.Length);
    
                }
            }
        }
    

    每当执行Byte[]receiveBytes时,我就会得到一个空的黑屏,什么都不会发生。有人能告诉我怎么解决这个问题吗?任何帮助都将不胜感激。非常感谢。

    3 回复  |  直到 7 年前
        1
  •  3
  •   Evk    7 年前

    这里有几个问题:

    1. 将数据发送到udp端口(通过 senddata() ) 之前 在该端口上启动一个侦听器,您只需执行一次,所以侦听器不可能收到它。

    2. 没有必要绑定 UdpClient 发送数据时发送到特定端口,尤其是发送到您正在与另一个用户侦听的同一端口 UDP客户端 . 仅使用 UdpClient udpClient = new UdpClient(); 让它使用任何可用端口进行发送。

    3. 由于您正在测试-无需将数据发送到外部ip,请改为发送到环回接口: udpClient.Connect(IPAddress.Loopback, 9999); .

    4. UDP客户端 机具 IDisposable ,因此在完成后进行处理。

    5. 你的 while (true) 循环将不起作用,因为您不处理 UDP客户端 ,所以在循环的第二次迭代中 UDP客户端 将尝试绑定到同一9999端口并失败,因为同一端口上已经有侦听器(您没有处理它)。

    您的代码和上面的补丁(显然这不是“生产”代码,所以我不会添加取消等内容,只有能够看到消息的补丁才会出现):

    static void senddata() {
        // send message every 100 ms
        while (true) {
            // wrap in using
            using (UdpClient udpClient = new UdpClient()) {
                // loopback
                udpClient.Connect(IPAddress.Loopback, 9999);
                Byte[] senddata1 = Encoding.ASCII.GetBytes("Hello World");
                udpClient.Send(senddata1, senddata1.Length);
            }
            Thread.Sleep(100);
        }
    }
    
    static void Main(string[] args) {
        // run sending in background
        Task.Run(() => senddata());
    
        try {
            // wrap in using
            using (UdpClient udpClient = new UdpClient(9999)) {
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                // move while loop here
                while (true) {
                    // this blocks until message is received
                    Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                    string returnData = Encoding.ASCII.GetString(receiveBytes);
                    Console.WriteLine(returnData);
                }
            }
        }
        catch (Exception e) {
            // do something meaningful
        }
    }
    
        2
  •  0
  •   peeebeee    7 年前

    看起来您没有输出收到的字符串。

    像这样的。。。。

    string result;
    result = returnData.ToString();
    Console.WriteLine(result);
    
        3
  •  0
  •   SoronelHaetir    7 年前

    尝试更改以下内容:

    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
    

    使用端口9999而不是随机端口。老实说,我有点惊讶,这没有引发异常。

    请注意,作为服务器绑定到随机端口并不是非典型的,但远程端需要某种方法来发现该随机端口I。。E、 ftp使用第二个端口进行实际文件数据传输的方式是,该端口号作为开始传输的消息的一部分发送。