为什么我的UDP广播失败了?

时间:2011-06-09 08:56:19

标签: .net c++-cli udp broadcast multicast

我正在尝试发送UDP广播,但wireshark没有报告任何流量。这是发送邮件的片段:

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));
    }
}

它报告它发送了数据(4个字节),为什么Wireshark没有看到流量?我已尝试使用另一个在同一端口上广播的应用程序,来自该应用程序的流量显示正常。

我错过了什么?

[编辑] 我刚刚在the UdpClient documentation底部发现一条帖子,指出在Windows 7计算机上发送到255.255.255.255不起作用。虽然整个o / s不是这样,或者从255.255.255.255的其他应用程序的广播失败了?

1 个答案:

答案 0 :(得分:4)

Windows 7以不同的方式处理255.255.255.255广播。更多信息:Send UDP broadcast on Windows 7