How to get the IP address of the machine in C #

Source: Internet
Author: User
How to get the IP address of the machine in IP address C #



Tips on how to use the DNS class and get the IP address of a machine

Introduced

This article is not a technical overview or a large discussion, but more like a collection of tips on how to get an IP address or host name. You can use the network API in Win32 API programming, which is similar in the. NET platform. The only difference is that you need to find and understand what namespaces (namespace) and classes are needed to accomplish this task. The network API exists in the System.Net namespace in the. NET platform. The DNS class in the System.Net namespace can be used to get the machine name and IP address. The DNS class provides a simple domain name interpretation function for a class. The DNS class provides support for handling Internet domain name (DNS) information. These returned information includes multiple IP addresses and host aliases. The returned list is a collection or an array of IPAddress objects. The following code shows how to get an IP address from a given host name.

Dnsutility Code
Namespace Nkutilities
{
Using System;
Using System.Net;

public class Dnsutility
{
public static int Main (string [] args)
{

String strhostname = new String ("");
if (args. Length = = 0)
{
First get the host name of the Local machine
Strhostname = DNS. GetHostName ();
Console.WriteLine ("Local Machine ' s Host Name:" + strhostname);
}
Else
{
Strhostname = Args[0];
}

Then get the IP address list through the host name
Iphostentry ipentry = DNS. gethostbyname (Strhostname);
IPAddress [] addr = ipentry.addresslist;

for (int i = 0; i < addr. Length; i++)
{
Console.WriteLine ("IP address {0}: {1}", I, addr[i]. ToString ());
}
return 0;
}
}
}
Explanation of the Code
If you want the host name of the local machine, you can call the GetHostName method without parameters. You can then call the GetHostByName method with the returned result as a parameter to get the Ipaddresses list, and then traverse the addresses collection to get the host's IP address.

Tips
Verify that the System.Net namespace is already included in your code, or the compiler will not know how to find the DNS class. Also, when you use VisualStudio.NET to create a project, make sure that your system already contains SYSTEM.NET.DLL. For more detailed information about DNS classes and System.Net namespaces, refer to the. NET SDK online documentation.



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.