多网卡JoinMulticastGroup报错“套接字操作尝试一个无法连接的主机”

使用UdpClient.JoinMulticastGroup (IPAddress)方法,在我win10的电脑上接收组播数据,若交换机上没发送数据,则程序会自动退出,并提示:“套接字操作尝试一个无法连接的主机”;winXP的电脑就没出现这个问题。

起初,以为是操作系统不同造成,但后来想起win10是双网口,无线和有线的,可能是win10不会自动选择网卡造成错误的。查了一下资料,果然有:

public void JoinMulticastGroup(
	IPAddress multicastAddr,
	IPAddress localAddress
)

udpReceive.JoinMulticastGroup(IPAddress.Parse(tsTB_IP.Text), ip_Local);

指定了localAddress之后,一直等待接收交换机上发的组播地址时,程序也不会退出的了。

顺便,备注一下,在多网卡中自动选取物理网卡的方法:

 using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;  

    public IPAddress ip_Local; //指定网卡发送

        ArrayList ip_AL = new ArrayList();

//获取网卡信息

        void Get_NetworkInterfaceInfo()

        {
            var interfaceList = NetworkInterface.GetAllNetworkInterfaces();
            //StringBuilder sb = new StringBuilder(512);
            int index = 0;
            // string sb = string.Empty;
            ip_AL = new ArrayList();

            int j = 0;
            foreach (var adapter in interfaceList)
            {
                string description = string.Empty;
                if (adapter.Description.Contains("Loopback")) continue; //跳过 环回地址127.0.0.1

                if (adapter.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up) //连接上的网口
                {
                    //listBox1.Items.Add(string.Format("Description: {0}:{1}", index++, adapter.Description));
                    //  listBox1.Items.Add(string.Format("OperationalStatus: {0}", adapter.OperationalStatus));

                    var addresses = adapter.GetIPProperties().UnicastAddresses;

                    foreach (var ip in addresses)
                    {
                        // j++;
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            j++;
                           // listBox1.Items.Add(string.Format("Address {0}:{1}", j, ip.Address.ToString()));
                            tsCB_Net.Items.Add(string.Format("{0} - {1} - {2}", j, adapter.Description, ip.Address.ToString()));
                            ip_AL.Add(ip.Address);
                        }
                    }
                  //  listBox1.Items.Add(" ");
                }
              }
        }


//网卡的combox手动选取

        private void tsCB_Net_SelectedIndexChanged(object sender, EventArgs e)
        {
            ip_Local = (IPAddress)ip_AL[tsCB_Net.SelectedIndex];
        }


  //主程序formLoad

        private void FormMain_Load(object sender, EventArgs e)

        {
            Get_NetworkInterfaceInfo();  //获取网卡信息
            for (int i = 0; i < tsCB_Net.Items.Count;i++ )
            {
                string s=tsCB_Net.Items[i].ToString();
                if (!tsCB_Net.Items[i].ToString().Contains("VMnet"))
                {
                    tsCB_Net.SelectedIndex = i;
                    ip_Local = (IPAddress)ip_AL[i];
                    break;
                }
                else
                {
                    tsCB_Net.SelectedIndex = 0;
                    ip_Local = (IPAddress)ip_AL[0];
                }
            }
        }


你可能感兴趣的:(c#)