C# socket绑定网卡连接

原因

因为我们做工业控制,电脑必然存在多个网口,常见的连接类型:

  1. 多个相机,每个相机会使用单独的千兆网卡,并且使用六类网线;
  2. 各个需要控制或者调用的下位机;
  3. 上传数据的上位机。

现场的网络环境复杂,存在很多不可空的因素,最常见的就是,连接下位机的socket会通过上传数据的网口连接到别的设备上,影响我们的设备正常运行。

结论

因此,我们需要绑定网卡来连接下位机。

代码如下

				string BindMac ="AAAAAAAAAA";//计划绑定的网卡的物理地址
				string IPLocal = string.Empty;
                IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                Console.WriteLine("Interface information for {0}.{1}     ",
                        computerProperties.HostName, computerProperties.DomainName);
                foreach (NetworkInterface adapter in nics)
                {
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    Console.WriteLine(adapter.Description);
                    Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
                    Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
                    Console.WriteLine("  Physical Address ........................ : {0}",
                                            adapter.GetPhysicalAddress().ToString());
                    Console.WriteLine("  Is receive only.......................... : {0}", adapter.IsReceiveOnly);
                    Console.WriteLine("  Multicast................................ : {0}", adapter.SupportsMulticast);

                    //获取单播地址集
                    UnicastIPAddressInformationCollection ipCollection = properties.UnicastAddresses;
                    foreach (UnicastIPAddressInformation ipadd in ipCollection)
                    {
                        if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            string ipv44 = ipadd.Address.ToString();//获取ip
                            string mac = adapter.GetPhysicalAddress().ToString();
                            Console.WriteLine("IP:{0}\tMac:{1}", ipv44, mac);
                            if (mac == BindMac)
                            {
                                IPLocal = ipv44;
                                break;
                            }
                        }
                        else if (ipadd.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            //本机IPV6 地址
                        }
                    }
                    if (!string.IsNullOrEmpty(IPLocal)) break;

                    Console.WriteLine();
                }

最终,我们使用获取到的“IPLocal”,绑定socket,然后通过下位机的IP地址来连接,确保连接的网卡正确。

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectSuccess = false;
socket.Bind(new IPEndPoint(IPAddress.Parse(IPLocal), 0)); //绑定本地通讯使用的网卡IP
socket.Connect(System.Net.IPAddress.Parse(IPAdress), Port);//连接下位机
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值