Why is my UDP broadcast failing?

别等时光非礼了梦想. 提交于 2019-12-23 13:02:06

问题


I'm trying to send a UDP broadcast but wireshark isn't reporting any traffic. Here's the snippet that does the sending:

void SendBroadcast()
{
    String^ ip = "255.255.255.255";
    int port = 30718;
    String^ message = "test";

    UdpClient^ udpClient = gcnew UdpClient();
    udpClient->EnableBroadcast = true;
    IPEndPoint^ ipDest = gcnew IPEndPoint(IPAddress::Parse(ip), port);
    cli::array<unsigned char>^ dgram = Encoding::ASCII->GetBytes(message);
    int bytesSent = udpClient->Send(dgram, dgram->Length, ipDest);

    if( bytesSent != message->Length )
    {
        // Failed to send
        Console::WriteLine(String::Format("Error: Failed to send all data (bytes sent: {0})", bytesSent));
    }
    else
    {
        Console::WriteLine(String::Format("Bytes sent: {0}", bytesSent));
    }
}

It reports that it's sent the data (4 bytes) so why doesn't Wireshark see the traffic? I've tried with another application which broadcasts on the same port and the traffic from that application shows up fine.

What am I missing?

[Edit] I just spotted a post on the bottom of the UdpClient documentation which states that sending to 255.255.255.255 on a windows 7 machine doesn't work. That can't be true of the o/s as a whole though or the broadcast from the other application to 255.255.255.255 would be failing?


回答1:


Windows 7 handles 255.255.255.255 broadcast in a different way. More info here: Send UDP broadcast on Windows 7



来源:https://stackoverflow.com/questions/6290329/why-is-my-udp-broadcast-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!