发送广播消息,从所有的网络适配器有的、消息、网络适配器

由网友(一见不钟情)分享简介:我有一个发送广播消息,并监听回应报文的应用程序。下面是code段。I have an application that sends broadcast messages and listens for response packets. Below is the code snippet.m_socket = n...

我有一个发送广播消息,并监听回应报文的应用程序。下面是code段。

I have an application that sends broadcast messages and listens for response packets. Below is the code snippet.

m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

m_socket.Bind(new IPEndPoint(IPAddress.Any, 2000));

m_socket.BeginSendTo(
                    buffer, 
                    0, 
                    buffer.Length, 
                    SocketFlags.None,
                    new IPEndPoint(IPAddress.Broadcast, 2000),
                    Callback), 
                    null
                    );

当我运行应用程序没有被发送广播消息。在我的机器我有三个网络适配器。一个是我的本地网络适配器,另两个是VMWare的虚拟网络适配器。当我运行我的应用程序,我可以看到(使用Wireshark的网络捕获)的广播消息是从的VMWare的网络适配器发送。

When I run the application the broadcast message was not being sent. On my machine I have three network adapters. One is my local network adapter and other two are VMWare network virtual adapters. When I run my application I can see (using wireshark network capture) that the broadcast message is being sent from one of the VMWare network adapters.

我想修改code,使广播信息将被从电脑上的所有网络适配器发送。什么是做到这一点的最好方法是什么?

I would like to modify the code so that the broadcast message will be sent from all network adapters on the pc. What is the best way to do that?

推荐答案

您可以使用下面的方法让所有的IP地址(和更多)。所以,你可以通过列表和绑定(如乔恩乙说)你想要的特定IP重复,当你发送的多播。

You can use the following to get all your IP Addresses (and a lot more). So you can iterate through the list and bind (like Jon B said) to the specific IP you want when you send out your multicast.

foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    foreach (var ua in i.GetIPProperties().UnicastAddresses)
        Console.WriteLine(ua.Address);
阅读全文

相关推荐

最新文章