Why is my UDP broadcast failing?
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.开发者_JAVA百科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?
Windows 7 handles 255.255.255.255 broadcast in a different way. More info here: Send UDP broadcast on Windows 7
精彩评论