发送广播消息,从所有的网络适配器 [英] Send broadcast message from all network adapters

查看:342
本文介绍了发送广播消息,从所有的网络适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送广播消息,并监听回应报文的应用程序。下面是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);

这篇关于发送广播消息,从所有的网络适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆