简体   繁体   English

C#UdpClient.receive()未收到对广播请求的响应

[英]C# UdpClient.receive() not receiving responses to broadcast request

I am trying to send data from my laptop to UDP server which communicates on port 30718. Data sending is correct, but when I want to receive response, program stucks on UdpClient.receive() method. 我试图将数据从笔记本电脑发送到在端口30718上进行通信的UDP服务器。数据发送正确,但是当我想接收响应时,程序卡在UdpClient.receive()方法上。 i tried to sniff network with wireshark and it seem that response is delivered correctly, but c# will not receive it. 我试图用Wireshark嗅探网络,并且似乎响应传递正确,但是C#不会收到它。 Here is my code: 这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        byte[] data = new byte[4];
        data[0] = 0x00;
        data[1] = 0x01;
        data[2] = 0x00;
        data[3] = 0xF6;

        byte[] rcvPacket = new Byte[1024];
        UdpClient client = new UdpClient();
        IPAddress address = IPAddress.Parse("255.255.255.255");
        client.EnableBroadcast = true;
        client.Connect(address, 30718);
        IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);


        client.Send(data, 4);
        rcvPacket = client.Receive(ref remoteIPEndPoint);

        string rcvData = Encoding.ASCII.GetString(rcvPacket);
        client.Close();  //close connection
    }

And here is output from wireshark (second picture is response to broadcast request): 这是wireshark的输出(第二张图片是对广播请求的响应): wireshark1

wireshark2

Does anybody have solution for fixing this problem? 有人有解决此问题的解决方案吗? Thank you. 谢谢。

Finally I found solution. 终于我找到了解决方案。 This is good example of sending broadcasts and receiving responses. 这是发送广播和接收响应的好例子。 C# How to do Network discovery using UDP Broadcast C#如何使用UDP广播进行网络发现

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

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