Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / C#
Article

How To Get IP Address Of A Machine

Rate me:
Please Sign up or sign in to vote.
3.22/5 (46 votes)
1 Feb 2001 829.3K   96   62
Tip on how to use DNS class and get IP address of a machine

Introduction

This article is not a technical overview or large discussion. It is like a collection of tips on how you can get the IP address or host name of a machine. In the Win32 API this could be accomplished using the NetWork API. And this is still true in the .NET framework. The only difference is finding and understanding what namespace and class to use to accomplish this task. In the .NET framework the NetWork API is available in the System.Net namespace. The DNS class in the System.Net namespace can be used to get the hostname of a machine or get the IP address if the hostname is already known. The DNS class provides a simple domain name resolution functionality. The DNS class is a static class that provides access to information from the Internet Domain Name System (DNS). The information returned includes multiple IP addresses and aliases if the host specified has more than one entry in the DNS database. The list is returned as a collection or an array of IPAddress objects. The following section is the code that shows how to obtain the IP address for 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)
          {
              // Getting Ip address of local machine...
              // First get the host name of local machine.
              strHostName = DNS.GetHostName ();
              Console.WriteLine ("Local Machine's Host Name: " +  strHostName);
          }
          else
          {
              strHostName = args[0];
          }
          
          // Then using host name, get the IP address list..
          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;
        }    
     }
}

What The Code Does

If you want to obtain the hostname of the local machine, then call the GetHostName method without a parameter. Then use the resulting hostname as a parameter to the GetHostByName method to get the list of IPAddresses that may be associated with the hostname. Then iterate through the collection of addresses to write out the IP Addresses associated with the hostname.

Reminders

Make sure that you include the System.Net namespace in your code; otherwise the compiler will not know where to look for the DNS class. Also when you use VisualStudio.NET for creating the project, make sure that you have the System.NET.Dll in your reference list. For more information on the DNS class and System.Net namespace, please refer to the online documentation for the .NET SDK.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralObtaining Remote IP and Port of a socket connection Pin
Simkin Extreme8-Oct-03 7:32
sussSimkin Extreme8-Oct-03 7:32 
QuestionHow To Know Machine's Name When &quot;Net Send Anynoumous&quot; Pin
Anonymous4-Oct-03 23:33
Anonymous4-Oct-03 23:33 
Generalfix the &quot;string&quot; Pin
Anonymous27-Aug-03 20:57
Anonymous27-Aug-03 20:57 
GeneralRe: fix the &quot;string&quot; Pin
samir411808-Nov-07 3:33
samir411808-Nov-07 3:33 
GeneralSuperman Pin
thiscat1-Jul-03 17:02
thiscat1-Jul-03 17:02 
GeneralRe: Superman Pin
HongMinhThi18-Sep-06 20:31
HongMinhThi18-Sep-06 20:31 
Generalfor() loop Pin
Anonymous29-Sep-02 23:39
Anonymous29-Sep-02 23:39 
GeneralHmm Pin
8-Mar-02 19:01
suss8-Mar-02 19:01 
Naveen must be a newbie... I feel sorry for the others that come to these websites to find examples that help teach, instead of bungle.

Better keep working at it NaveenHmmm | :| Hmmm | :|
GeneralCompiling Pin
4-Nov-01 6:31
suss4-Nov-01 6:31 
GeneralRe: Compiling Pin
Anonymous7-Jul-03 4:31
Anonymous7-Jul-03 4:31 
GeneralRe: Compiling Pin
LuckySmoke23-Mar-07 4:31
LuckySmoke23-Mar-07 4:31 
GeneralRe: System.net.dll missing Pin
tlongman6-Oct-04 7:51
tlongman6-Oct-04 7:51 
GeneralCompiling with csc w/o VS Pin
22-Aug-01 5:43
suss22-Aug-01 5:43 
GeneralRe: Compiling with csc w/o VS Pin
Anonymous15-May-04 22:06
Anonymous15-May-04 22:06 
GeneralGrammer Pin
18-May-01 11:33
suss18-May-01 11:33 
GeneralRe: Grammer Pin
John Churchill28-May-01 8:32
John Churchill28-May-01 8:32 
GeneralRe: Grammer Pin
28-May-01 9:30
suss28-May-01 9:30 
GeneralRe: Grammer Pin
16-Aug-01 0:53
suss16-Aug-01 0:53 
GeneralRe: Grammer Pin
Anonymous5-Aug-04 6:22
Anonymous5-Aug-04 6:22 
GeneralRe: Grammer Pin
Moe Howard20-Jun-05 3:33
Moe Howard20-Jun-05 3:33 
GeneralAbsolutely Pin
NormDroid1-Dec-00 9:21
professionalNormDroid1-Dec-00 9:21 
GeneralRe: Absolutely Pin
1-Dec-00 12:21
suss1-Dec-00 12:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.