public void setLocalAddr(TextBox addresses) { //first get out IP address and store it for later use string str = ""; stationConsole.Text += "Determine Local Addr" + Environment.NewLine; System.Net.NetworkInformation.NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); List <string> ipAddr = new List <string>(); foreach (NetworkInterface adapter in nics) { NetworkInterfaceType networkType = adapter.NetworkInterfaceType; if (networkType.Equals(NetworkInterfaceType.Ethernet)) { foreach (var x in adapter.GetIPProperties().UnicastAddresses) { if (x.Address.AddressFamily == AddressFamily.InterNetwork && x.IsDnsEligible) { Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString()); ipAddr.Add(x.Address.ToString()); } } address = adapter.GetPhysicalAddress(); byte[] bytes = address.GetAddressBytes(); for (int i = 0; i < bytes.Length; i++) { // Display the physical address in hexadecimal. addresses.Text += bytes[i].ToString("X2"); Console.Write("{0}", bytes[i].ToString("X2")); // Insert a hyphen after each byte, unless we are at the end of the // address. if (i != bytes.Length - 1) { Console.Write("-"); addresses.Text += "-"; } } addresses.Text += Environment.NewLine; } } int count = ipAddr.Count(); for (int i = 0; i < count; i++) { byte[] addrBytes = IPAddress.Parse(ipAddr[i]).GetAddressBytes(); if (addrBytes[0] == 0xC0) { localAddr = IPAddress.Parse(ipAddr[i]); addresses.Text += localAddr; break; } else { stationConsole.Text += "ERROR Unable to determine self IP address!!!" + Environment.NewLine; } } // Print SharpPcap version string ver = SharpPcap.Version.VersionString; stationConsole.Text += "SharpPcap {0}, Example1.IfList.cs" + ver + Environment.NewLine; // Retrieve the device list devices = SharpPcap.CaptureDeviceList.Instance; // If no devices were found print an error if (devices.Count < 1) { stationConsole.Text += "No devices were found on this machine" + Environment.NewLine; return; } else { stationConsole.Text += "Device count = " + devices.Count + Environment.NewLine; } //Console.WriteLine("\nThe following devices are available on this machine:"); //Console.WriteLine("----------------------------------------------------\n"); // Print out the available network devices //foreach (ICaptureDevice dev in devices) // Console.WriteLine("{0}\n", dev.ToString()); // Open the device for capturing int readTimeoutMilliseconds = 1000; ICaptureDevice device; filter = "port 20000"; string localInterfaceUsed = address.ToString(); for (ushort i = 0; i < devices.Count; i++) { device = devices[i]; device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds); var macAddress = device.MacAddress; string hwAddr = macAddress.ToString(); if (hwAddr.Equals(localInterfaceUsed)) { transmitDevice = device; } } }
public void setLocalAddr(TextBox addresses) { //first get out IP address and store it for later use string str = ""; stationConsole.Text += "Determine Local Addr" + Environment.NewLine; System.Net.NetworkInformation.NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); List <string> ipAddr = new List <string>(); SharpPcap.CaptureDeviceList devices; foreach (NetworkInterface adapter in nics) { NetworkInterfaceType networkType = adapter.NetworkInterfaceType; if (networkType.Equals(NetworkInterfaceType.Ethernet)) { foreach (var x in adapter.GetIPProperties().UnicastAddresses) { if (x.Address.AddressFamily == AddressFamily.InterNetwork && x.IsDnsEligible) { Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString()); ipAddr.Add(x.Address.ToString()); } } etherAddress = adapter.GetPhysicalAddress(); byte[] bytes = etherAddress.GetAddressBytes(); for (int i = 0; i < bytes.Length; i++) { // Display the physical address in hexadecimal. addresses.Text += bytes[i].ToString("X2"); Console.Write("{0}", bytes[i].ToString("X2")); // Insert a hyphen after each byte, unless we are at the end of the // address. if (i != bytes.Length - 1) { Console.Write("-"); addresses.Text += "-"; } } addresses.Text += Environment.NewLine; } } int count = ipAddr.Count(); for (int i = 0; i < count; i++) { byte[] addrBytes = IPAddress.Parse(ipAddr[i]).GetAddressBytes(); if (addrBytes[0] == 0xC0) { localIPAddr = IPAddress.Parse(ipAddr[i]); addresses.Text += localIPAddr; break; } else { stationConsole.Text += "ERROR Unable to determine self IP address!!!" + Environment.NewLine; } } // Retrieve the device list devices = SharpPcap.CaptureDeviceList.Instance; // If no devices were found print an error if (devices.Count < 1) { stationConsole.Text += "No devices were found on this machine" + Environment.NewLine; return; } }
/// <summary> /// 获取当前本地连接列表,根据网络接口类型,和操作状态 /// </summary> /// <param name="type">网络接口类型</param> /// <param name="ipEnabled">网络接口的操作状态</param> public static List<NetworkVO> GetNetworkList(NetworkInterfaceType type, OperationalStatus status) { List<NetworkVO> list = new List<NetworkVO>(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { //过滤网络接口类型 if (!NetworkInterfaceType.Unknown.Equals(type) && !type.Equals(adapter.NetworkInterfaceType)) { logger.Debug("跳过的其他类型网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } //过滤网络接口的操作状态 if (!status.Equals(adapter.OperationalStatus)) { logger.Debug("跳过的不是上行网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } NetworkVO vo = new NetworkVO(); vo.IpEnabled = true; IPInterfaceProperties property = adapter.GetIPProperties(); vo.DnsHostName = Dns.GetHostName();//本机名 vo.Name = adapter.Name; vo.Description = adapter.Description; vo.Speed = adapter.Speed; //macAddress if (adapter.GetPhysicalAddress() != null && adapter.GetPhysicalAddress().ToString().Length > 0) { char[] mac = adapter.GetPhysicalAddress().ToString().ToCharArray(); vo.MacAddress = mac[0] + mac[1] + "-" + mac[2] + mac[3] + "-" + mac[4] + mac[5] + "-" + mac[6] + mac[7] + "-" + mac[8] + mac[9] + "-" + mac[10] + mac[11]; } //ipAddress subnetMask if (property.UnicastAddresses != null && property.UnicastAddresses.Count > 0) { foreach (UnicastIPAddressInformation ip in property.UnicastAddresses) { if (ip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { if (ip.Address != null) { vo.Address = ip.Address.ToString(); } if (ip.IPv4Mask != null) { vo.SubnetMask = ip.IPv4Mask.ToString(); } } } } // gateway if (property.GatewayAddresses != null && property.GatewayAddresses.Count > 0) { foreach (GatewayIPAddressInformation uip in property.GatewayAddresses) { if (uip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.Gateway = uip.Address.ToString(); } } } // dns server if (property.DnsAddresses != null && property.DnsAddresses.Count > 0) { vo.DnsServers = new List<string>(); foreach (IPAddress ip in property.DnsAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DnsServers.Add(ip.ToString()); } } } // dhcp server if (property.DhcpServerAddresses != null && property.DhcpServerAddresses.Count > 0) { foreach (IPAddress ip in property.DhcpServerAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DhcpServer = ip.ToString(); vo.DhcpEnabled = true; } } } else { vo.DhcpEnabled = false; } list.Add(vo); } return list; }
/// <summary> /// 获取当前本地连接列表,根据网络接口类型,和操作状态 /// </summary> /// <param name="type">网络接口类型</param> /// <param name="ipEnabled">网络接口的操作状态</param> public static List <NetworkVO> GetNetworkList(NetworkInterfaceType type, OperationalStatus status) { List <NetworkVO> list = new List <NetworkVO>(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { //过滤网络接口类型 if (!NetworkInterfaceType.Unknown.Equals(type) && !type.Equals(adapter.NetworkInterfaceType)) { logger.Debug("跳过的其他类型网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } //过滤网络接口的操作状态 if (!status.Equals(adapter.OperationalStatus)) { logger.Debug("跳过的不是上行网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } NetworkVO vo = new NetworkVO(); vo.IpEnabled = true; IPInterfaceProperties property = adapter.GetIPProperties(); vo.DnsHostName = Dns.GetHostName();//本机名 vo.Name = adapter.Name; vo.Description = adapter.Description; vo.Speed = adapter.Speed; //macAddress if (adapter.GetPhysicalAddress() != null && adapter.GetPhysicalAddress().ToString().Length > 0) { char[] mac = adapter.GetPhysicalAddress().ToString().ToCharArray(); vo.MacAddress = mac[0] + mac[1] + "-" + mac[2] + mac[3] + "-" + mac[4] + mac[5] + "-" + mac[6] + mac[7] + "-" + mac[8] + mac[9] + "-" + mac[10] + mac[11]; } //ipAddress subnetMask if (property.UnicastAddresses != null && property.UnicastAddresses.Count > 0) { foreach (UnicastIPAddressInformation ip in property.UnicastAddresses) { if (ip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { if (ip.Address != null) { vo.Address = ip.Address.ToString(); } if (ip.IPv4Mask != null) { vo.SubnetMask = ip.IPv4Mask.ToString(); } } } } // gateway if (property.GatewayAddresses != null && property.GatewayAddresses.Count > 0) { foreach (GatewayIPAddressInformation uip in property.GatewayAddresses) { if (uip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.Gateway = uip.Address.ToString(); } } } // dns server if (property.DnsAddresses != null && property.DnsAddresses.Count > 0) { vo.DnsServers = new List <string>(); foreach (IPAddress ip in property.DnsAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DnsServers.Add(ip.ToString()); } } } // dhcp server if (property.DhcpServerAddresses != null && property.DhcpServerAddresses.Count > 0) { foreach (IPAddress ip in property.DhcpServerAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DhcpServer = ip.ToString(); vo.DhcpEnabled = true; } } } else { vo.DhcpEnabled = false; } list.Add(vo); } return(list); }