Click here to Skip to main content
Click here to Skip to main content

How To Get IP Address Of A Machine

, 1 Feb 2001
Rate this:
Please Sign up or sign in to vote.
Tip on how to use DNS class and get IP address of a machine
<!-- Add the rest of your HTML here -->

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

Share

About the Author

Naveen K Kohli

United States United States
No Biography provided

Comments and Discussions

 
GeneralThis is very good article. PinmemberJayesh Sorathia7-Aug-12 21:55 
QuestionConsole closing PinmemberArjun Menon U.K30-Jun-12 2:36 
GeneralMy vote of 2 Pinmemberhlsnnet2-Apr-12 20:52 
Questionreferences of your article PinmvpMd. Marufuzzaman25-Dec-11 7:41 
GeneralInetAddress PinmemberPrerna Sancheti6-May-11 5:00 
GeneralMy vote of 2 PinmemberRaviSant6-Jan-11 0:34 
GeneralMy vote of 3 PinmemberLibin Jose chemperi6-Dec-10 23:15 
GeneralMy vote of 1 PinmemberJörgen Sigvardsson9-Jun-09 20:11 
GeneralRe: My vote of 1 PinmemberPRMan!!!24-Sep-09 8:26 
GeneralRe: My vote of 1 PinmemberDizZ21-May-10 6:07 
You can try with :
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().UnicastAddresses[0].Address
 
The first index is if you have multiple network cards and the second index is if you have multiple ip addresses on the same network card
 
Good luck
AnswerRe: My vote of 1 PinmemberKenneth McKnight15-Jun-10 21:41 
General[newbie] Pinmemberjon_8029-Mar-09 4:05 
GeneralRe: [newbie] PinmemberByteBlocks29-Mar-09 7:27 
GeneralRe: [newbie] [modified] Pinmemberjon_8030-Mar-09 12:10 
GeneralRe: [newbie] PinmemberByteBlocks30-Mar-09 14:00 
Generalhello Pinmemberjr_jamesrobert016-Jun-08 11:45 
Questionhow how u there Pinmemberjr_jamesrobert016-Jun-08 7:28 
QuestionWhat I'd really like to see is... Pinmembermycal27-Sep-07 8:24 
GeneralIPv6 (::1) issue with this code. Pinmemberdurkpurk28-Jul-07 1:03 
GeneralRe: IPv6 (::1) issue with this code. PinmemberMonir Sabbagh21-Apr-09 23:16 
GeneralRe: IPv6 (::1) issue with this code. PinmemberIvanGB0016-Jul-12 3:03 
Generalquestion in getting system information Pinmembervarghesh17-Mar-07 21:18 
GeneralGet all IPAddress in the network PinmemberWakwak Chimera3-Jan-07 19:25 
QuestionHow To Get IP Address &amp; MAC address Of (Barcode Printer) of the Network? PinmemberJan Palmer3-Jan-07 18:46 
GeneralIP Collection PinmemberDragon Slayer8-Jul-06 1:47 
GeneralRe: IP Collection PinmemberDizZ21-May-10 6:16 
Questionclient/server on the same computer Pinmemberraniam25-Apr-06 19:38 
AnswerRe: client/server on the same computer Pinmemberpoduvajte21-Sep-07 8:11 
GeneralDNS should be Dns Pinmemberjinzhecheng8-Feb-06 5:14 
QuestionIP address related to a network connection type PinmemberPriyav29-Jan-06 23:39 
GeneralFramework 2.0 names Pinmemberrent0n16-Jan-06 23:56 
GeneralGet External IP PinmemberVitoto25-Jun-05 20:56 
Generalthanks! Pinmemberscottfm1-Apr-05 7:11 
QuestionHow can i see all IP address in local LAN PinmemberRattapol6-Jan-05 20:59 
AnswerRe: How can i see all IP address in local LAN PinsussMarcelo Amaolo21-Jan-05 7:26 
AnswerHow can i see all IP address in local LAN Pinmembersanjeev goyal20-Apr-05 6:02 
Generala question about GetHostByAddress Pinmemberstej_cz6-Dec-04 6:26 
GeneralCallback PinmemberFrisken8-Jul-04 6:54 
GeneralThanks a lot :D Pinmembermielz15-Jun-04 23:49 
GeneralObtaining Remote IP and Port of a socket connection PinsussSimkin Extreme8-Oct-03 7:32 
QuestionHow To Know Machine's Name When &quot;Net Send Anynoumous&quot; PinsussAnonymous4-Oct-03 23:33 
Generalfix the &quot;string&quot; PinsussAnonymous27-Aug-03 20:57 
GeneralRe: fix the &quot;string&quot; Pinmembersamir411808-Nov-07 3:33 
GeneralSuperman PinsussNguyen Van Tinh1-Jul-03 17:02 
GeneralRe: Superman PinmemberHongMinhThi18-Sep-06 20:31 
Generalfor() loop PinsussAnonymous29-Sep-02 23:39 
GeneralHmm PinmemberLiquid Sky8-Mar-02 19:01 
GeneralCompiling PinmemberAnonymous4-Nov-01 6:31 
GeneralRe: Compiling PinsussAnonymous7-Jul-03 4:31 
GeneralRe: Compiling PinmemberLuckySmoke23-Mar-07 4:31 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 2 Feb 2001
Article Copyright 2000 by Naveen K Kohli
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid