C # automatically select the most suitable IP address in the system

Source: Internet
Author: User

C # automatically select the most suitable IP address in the system
This is because this problem has been encountered many times for a long time, but it is not taken seriously. This time I encountered this old problem, but Baidu did not get it through a circle, if you don't have any big bull around you, you can "summarize" a set of methods and record it for yourself, secondly, it would be nice to have a friend who sees and solves the same problem for him. Cause: Any network program, Coder seems inevitable. How can your code pick out the most suitable IP address for communication on different machines, because if you open an application to allow users to choose which IP address and port to communicate, it seems that this is a low-end program, how low-energy programmers, so you must automatically choose, user 0 perception, just like great QQ. I also encountered this problem again. If the Q group did not return to Baidu, I used the methods and Attributes provided in the Framework and summarized a "rule". Next I went straight to the topic. The current machine may be a dual-nic, because various software and drivers have multiple virtual NICs, so my "rule" does not directly judge whether the IP address meets the requirements, start with NetworkInterface. The following is a summary: 1. obtain all local NetworkInterface objects. 2. the property Type is preferably Wireless80211, Ethernet, Fddi, Ppp, etc., because according to MSDN, these types will be common interface types of ordinary home computers, but this judgment is not accurate, therefore, it is only used as an auxiliary judgment condition; 3. the OperationalStatus attribute should be Up. According to MSDN, only Up indicates that the network interface can send and receive data normally, which can also be used as a network adapter (network) A condition for availability; 4. use GetIPProperties (). the UnicastAddresses method obtains all unicast addresses under this network interface, which allows you to obtain network IP addresses. du of the IP address The plicateAddressDetectionState attribute should be Preferred, indicating that the address is valid, that is, the IP address can be used normally; 6. the value of PrefixOrigin is Dhcp or Manual, indicating that the IP prefix is assigned by Dhcp or manually specified. the value of SuffixOrigin is OriginDhcp or Manual, indicating that the IP suffix is assigned by DHCP or manually specified. In summary, some special addresses such as 169 and 127.0.0.1 can be excluded, as well as some network interfaces (some network interfaces do not have addresses, and some others only have IPV6 addresses ), however, I feel that this method is not a proper path. I hope someone with experience can give me some advice. The following is the result of testing and code: lazy code:

var interfaceList = NetworkInterface.GetAllNetworkInterfaces();            StringBuilder sb = new StringBuilder(512);            int index = 0;            string message = string.Empty;            foreach(var item in interfaceList)            {                index++;                sb.AppendLine(string.Format("Interface{0}: {1}", index, item.Name));                sb.AppendLine(string.Format("Description: {0}", item.Description));                sb.AppendLine(string.Format("ID: {0}", item.Id));                sb.AppendLine(string.Format("Type: {0}", item.NetworkInterfaceType));                sb.AppendLine(string.Format("OperationalStatus: {0}", item.OperationalStatus));                sb.AppendLine(string.Format("IsReceiveOnly: {0}", item.IsReceiveOnly));                sb.AppendLine(string.Format("Speed: {0}", item.Speed));                sb.AppendLine(string.Format("SupportMulticast: {0}", item.SupportsMulticast));                sb.AppendLine(string.Format("SupportIPV4: {0}", item.Supports(NetworkInterfaceComponent.IPv4)));                sb.AppendLine(string.Format("IP Properties:"));                var addresses = item.GetIPProperties().UnicastAddresses;                int j = 0;                foreach (var ip in addresses)                {                    j++;                    sb.AppendLine(string.Format("Address{0}: {1}({2})", j, ip.Address.ToString(),ip.Address.AddressFamily));                    sb.AppendLine(string.Format("IPV4 Mask: {0}", ip.IPv4Mask));                    sb.AppendLine(string.Format("PrefixOrigin: {0}", ip.PrefixOrigin));                    sb.AppendLine(string.Format("SuffixOrigin: {0}", ip.SuffixOrigin));                    sb.AppendLine(string.Format("DuplicateAddressDetectionState: {0}", ip.DuplicateAddressDetectionState));                }                sb.AppendLine();            }            textBox1.Text = sb.ToString();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.