How i can get 100% real IP and MAC address of PC u

2020-04-18 07:53发布

问题:

I am getting IP and MAC of the PC for socket connection but the problem is in PC there are 2 3 adapters for network interface like I have Wi-fi and LAN also so sometime it's giving wrong IP Address and my socket connection goes wrong.

Following is my code to get IP Address

public static string GetIPAddress()
{
    string Response = string.Empty;
    try
    {
        var ni = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface item in ni)
        {
            if (item.OperationalStatus == OperationalStatus.Up) //&& item.NetworkInterfaceType == ?
            {
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork & !IPAddress.IsLoopback(ip.Address))
                    {
                        Response = ip.Address.ToString();
                    }
                }
            }
        }
    }
    catch (Exception)
    {

    }
    return Response;
}

回答1:

You are probably reading the IPv6 address.
Or, if the Ethernet interface OperationalStatus is Up but not connected - possibly because of a missing DHCP reference, the Automatic Private IP Address with a Link Layer Suffix (e.g. 169.254.0.0169.254.80.104).

One possible way to determine whether the IP Address is a working IP, is testing the IsDnsEligible property of the UnicastIPAddressInformation class, which can be accessed using the NetworkInterface GetIPProperties() method.

This will exclude all non-routable addresses (Private, Loopback etc.).
Also, if you know that your connection will have to use a specific transport (Ethernet, WiFi, Tunnel, Atm etc.), you can also test the NetworkInterface NetworkInterfaceType property. See the enumeration values: NetworkInterfaceType Enumeration.

using System.Net.NetworkInformation;

NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();

//Get all the IPv4 addresses
List<NetworkInterface> IPV4Interfaces = Interfaces
    .Where(IF => (IF.Supports(NetworkInterfaceComponent.IPv4)))
    .ToList();

//Utility list which collects informations on all the IPv4 interfaces
List<NetWorkInterfacesInfo> NetInterfacesInfo = IPV4Interfaces.Select(IF => new NetWorkInterfacesInfo()
{
    Name = IF.Name,
    Description = IF.Description,
    Status = IF.OperationalStatus,
    Speed = IF.Speed,
    InterfaceType = IF.NetworkInterfaceType,
    MAC = IF.GetPhysicalAddress(),
    DnsServers = IF.GetIPProperties().DnsAddresses.Select(ipa => ipa).ToList(),
    IPAddresses = IF.GetIPProperties().UnicastAddresses.Select(ipa => ipa.Address).ToList(),
    Gateways = IF.GetIPProperties().GatewayAddresses.Select(ipa => ipa.Address).ToList(),
    DHCPEnabled = IF.GetIPProperties().GetIPv4Properties().IsDhcpEnabled,
    BytesSent = IF.GetIPStatistics().BytesSent,
    BytesReceived = IF.GetIPStatistics().BytesReceived
}).ToList();


//Utility container class for the NetInterfacesInfo list
public class NetWorkInterfacesInfo
{
    public string Name { get; set; }
    public string Description { get; set; }
    public PhysicalAddress MAC { get; set; }
    public List<IPAddress> IPAddresses { get; set; }
    public List<IPAddress> DnsServers { get; set; }
    public List<IPAddress> Gateways { get; set; }
    public bool DHCPEnabled { get; set; }
    public OperationalStatus Status { get; set; }
    public NetworkInterfaceType InterfaceType { get; set; }
    public Int64 Speed { get; set; }
    public Int64 BytesSent { get; set; }
    public Int64 BytesReceived { get; set; }
}

Now you can identify a valid/routable IP Address filtering the list:

//Extract the Dns Eligible IP addresses
if (IPV4Interfaces != null)
{
    List<UnicastIPAddressInformation> RoutableIpAddresses =
        IPV4Interfaces.Select(IF => IF.GetIPProperties().UnicastAddresses.Last())
                      .Where(UniIP => UniIP.IsDnsEligible).ToList();
}

Or use more filters to select, for example, all routable IP Addresses with a Wireless transport:

if (IPV4Interfaces != null)
{
    List<UnicastIPAddressInformation> RoutableIpAddresses =
        IPV4Interfaces.Where(IF => IF.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                      .Select(IF => IF.GetIPProperties().UnicastAddresses.Last())
                      .Where(UniIP => UniIP.IsDnsEligible).ToList();
}


标签: c# ip