在我的所有IP上发送UDP多播数据包

时间:2013-03-06 15:23:44

标签: c# sockets network-programming udp multicast

我正在尝试在我的所有网络接口(2个LAN,一个wifi)上发送多播数据包。我最初遵循了tutorial

我遇到的问题是,似乎数据包似乎只有我的一个IP地址。

这是我目前的代码。

private static void SendOnAllCards(int port, String address)
{
    using (Socket mSendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    {
        mSendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                    new MulticastOption(IPAddress.Parse(address)));
        mSendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 255);
        mSendSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        mSendSocket.Bind(new IPEndPoint(IPAddress.Any, port));
        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(address), port);
        mSendSocket.Connect(ipep);


        byte[] bytes = Encoding.ASCII.GetBytes("This is my welcome message");
        mSendSocket.Send(bytes, bytes.Length, SocketFlags.None);
    }
}

我尝试手动完成:

private static void SendOnAllCards(int port, string remoteAddressSrc)
{
    foreach (IPAddress remoteAddress in Dns.GetHostAddresses(Dns.GetHostName()).Where(i=>i.AddressFamily == AddressFamily.InterNetwork))
    {

        using (Socket mSendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
        {
            mSendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                        new MulticastOption(IPAddress.Parse(remoteAddressSrc)));
            mSendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 255);
            mSendSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            mSendSocket.Bind(new IPEndPoint(IPAddress.Any, port));
            IPEndPoint ipep = new IPEndPoint(remoteAddress, port);
            mSendSocket.Connect(ipep);


            byte[] bytes = Encoding.ASCII.GetBytes("This is my welcome message");
            mSendSocket.Send(bytes, bytes.Length, SocketFlags.None);
        }
    }
}

这有效,但这意味着我要创建尽可能多的套接字我已经知道IP(在这个示例中我在每次发送时创建它们,但它只是一个测试),我不喜欢我的方式获得我所有的IP。

那么这样做的正确方法是什么?

编辑第二个红利问题:为什么当我在Connect中指定本地IP时指定远程地址,但不在Bind上?

2 个答案:

答案 0 :(得分:1)

似乎我们必须在网络接口上进行迭代,在本地IP上进行绑定并执行发送* *调用:

public void SendTestMessage()
{
    foreach (IPAddress localIp in
        Dns.GetHostAddresses(Dns.GetHostName()).Where(i => i.AddressFamily == AddressFamily.InterNetwork))
    {
        IPAddress ipToUse = localIp;
        using (var mSendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
        {
            mSendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                        new MulticastOption(_multicastIp, localIp));
            mSendSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 255);
            mSendSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            mSendSocket.MulticastLoopback = true;
            mSendSocket.Bind(new IPEndPoint(ipToUse, _port));


            byte[] bytes = Encoding.ASCII.GetBytes("This is my welcome message");
            var ipep = new IPEndPoint(_multicastIp, _port);
            mSendSocket.SendTo(bytes, ipep);
        }
    }
}

答案 1 :(得分:0)

您可以使用WMI类Win32_NetworkAdapter找到网络适配器。您可能必须优化查询实例的结果,因为Windows和第三方软件都创建了许多“虚拟适配器”。 我写了一个快速的posh脚本来使用WMI来检索适配器。这应该只显示具有IP地址的适配器。这只是为了显示您可以查询的WMI信息,以便检索激活的adapeters。

$networkadapters = @(Get-CimInstance -ClassName Win32_NetworkAdapter -Property *)
$networkadapters_configs = @($networkadapters | Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration)

0..($networkadapters.Length-1) | %{
    if($networkadapters_configs[$_].IPAddress -gt '') {
        Write-Output "Adapter #$($_)" 
        Write-Output $networkadapters[$_]
        Write-Output "Config #$($_)" 
        Write-Output $networkadapters_configs[$_]
    }
}