Click here to Skip to main content
13,292,205 members (57,636 online)
Click here to Skip to main content
Add your own
alternative version


95 bookmarked
Posted 30 Nov 2000

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 -->


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);
              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.


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.


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


About the Author

Naveen K Kohli
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThis is very good article. Pin
Jayesh Sorathia7-Aug-12 22:55
memberJayesh Sorathia7-Aug-12 22:55 
QuestionConsole closing Pin
Arjun Menon U.K30-Jun-12 3:36
memberArjun Menon U.K30-Jun-12 3:36 
AnswerRe: Console closing Pin
jleol16-Sep-16 3:05
memberjleol16-Sep-16 3:05 
GeneralMy vote of 2 Pin
hlsnnet2-Apr-12 21:52
memberhlsnnet2-Apr-12 21:52 
Questionreferences of your article Pin
Md. Marufuzzaman25-Dec-11 8:41
mvpMd. Marufuzzaman25-Dec-11 8:41 
GeneralInetAddress Pin
Prerna Sancheti6-May-11 6:00
memberPrerna Sancheti6-May-11 6:00 
GeneralMy vote of 2 Pin
RaviSant6-Jan-11 1:34
memberRaviSant6-Jan-11 1:34 
GeneralMy vote of 3 Pin
Libin Jose chemperi7-Dec-10 0:15
memberLibin Jose chemperi7-Dec-10 0:15 
GeneralMy vote of 1 Pin
Jörgen Sigvardsson9-Jun-09 21:11
memberJörgen Sigvardsson9-Jun-09 21:11 
GeneralRe: My vote of 1 Pin
PRMan!!!24-Sep-09 9:26
memberPRMan!!!24-Sep-09 9:26 
GeneralRe: My vote of 1 Pin
DizZ21-May-10 7:07
memberDizZ21-May-10 7:07 
AnswerRe: My vote of 1 Pin
Kenneth McKnight15-Jun-10 22:41
memberKenneth McKnight15-Jun-10 22:41 
General[newbie] Pin
jon_8029-Mar-09 5:05
memberjon_8029-Mar-09 5:05 
GeneralRe: [newbie] Pin
ByteBlocks29-Mar-09 8:27
memberByteBlocks29-Mar-09 8:27 
GeneralRe: [newbie] [modified] Pin
jon_8030-Mar-09 13:10
memberjon_8030-Mar-09 13:10 
GeneralRe: [newbie] Pin
ByteBlocks30-Mar-09 15:00
memberByteBlocks30-Mar-09 15:00 
Generalhello Pin
jr_jamesrobert016-Jun-08 12:45
memberjr_jamesrobert016-Jun-08 12:45 
Questionhow how u there Pin
jr_jamesrobert016-Jun-08 8:28
memberjr_jamesrobert016-Jun-08 8:28 
QuestionWhat I'd really like to see is... Pin
mycal27-Sep-07 9:24
membermycal27-Sep-07 9:24 
GeneralIPv6 (::1) issue with this code. Pin
durkpurk28-Jul-07 2:03
memberdurkpurk28-Jul-07 2:03 
GeneralRe: IPv6 (::1) issue with this code. Pin
Monir Sabbagh22-Apr-09 0:16
memberMonir Sabbagh22-Apr-09 0:16 
GeneralRe: IPv6 (::1) issue with this code. Pin
IvanGB0016-Jul-12 4:03
memberIvanGB0016-Jul-12 4:03 
Generalquestion in getting system information Pin
varghesh17-Mar-07 22:18
membervarghesh17-Mar-07 22:18 
GeneralGet all IPAddress in the network Pin
Wakwak Chimera3-Jan-07 20:25
memberWakwak Chimera3-Jan-07 20:25 
QuestionHow To Get IP Address &amp; MAC address Of (Barcode Printer) of the Network? Pin
Jan Palmer3-Jan-07 19:46
memberJan Palmer3-Jan-07 19:46 
GeneralIP Collection Pin
Dragon Slayer8-Jul-06 2:47
memberDragon Slayer8-Jul-06 2:47 
GeneralRe: IP Collection Pin
DizZ21-May-10 7:16
memberDizZ21-May-10 7:16 
Questionclient/server on the same computer Pin
raniam25-Apr-06 20:38
memberraniam25-Apr-06 20:38 
AnswerRe: client/server on the same computer Pin
poduvajte21-Sep-07 9:11
memberpoduvajte21-Sep-07 9:11 
GeneralDNS should be Dns Pin
jinzhecheng8-Feb-06 6:14
memberjinzhecheng8-Feb-06 6:14 
QuestionIP address related to a network connection type Pin
Priyav30-Jan-06 0:39
memberPriyav30-Jan-06 0:39 
GeneralFramework 2.0 names Pin
rent0n17-Jan-06 0:56
memberrent0n17-Jan-06 0:56 
GeneralGet External IP Pin
Vitoto25-Jun-05 21:56
memberVitoto25-Jun-05 21:56 
Generalthanks! Pin
scottfm1-Apr-05 8:11
memberscottfm1-Apr-05 8:11 
QuestionHow can i see all IP address in local LAN Pin
Rattapol6-Jan-05 21:59
memberRattapol6-Jan-05 21:59 
AnswerRe: How can i see all IP address in local LAN Pin
Marcelo Amaolo21-Jan-05 8:26
sussMarcelo Amaolo21-Jan-05 8:26 
AnswerHow can i see all IP address in local LAN Pin
sanjeev goyal20-Apr-05 7:02
membersanjeev goyal20-Apr-05 7:02 
Generala question about GetHostByAddress Pin
stej_cz6-Dec-04 7:26
memberstej_cz6-Dec-04 7:26 
GeneralCallback Pin
Frisken8-Jul-04 7:54
memberFrisken8-Jul-04 7:54 
GeneralThanks a lot :D Pin
mielz16-Jun-04 0:49
membermielz16-Jun-04 0:49 
GeneralObtaining Remote IP and Port of a socket connection Pin
Simkin Extreme8-Oct-03 8:32
sussSimkin Extreme8-Oct-03 8:32 
QuestionHow To Know Machine's Name When &quot;Net Send Anynoumous&quot; Pin
Anonymous5-Oct-03 0:33
sussAnonymous5-Oct-03 0:33 
Generalfix the &quot;string&quot; Pin
Anonymous27-Aug-03 21:57
sussAnonymous27-Aug-03 21:57 
GeneralRe: fix the &quot;string&quot; Pin
samir411808-Nov-07 4:33
membersamir411808-Nov-07 4:33 
GeneralSuperman Pin
Nguyen Van Tinh1-Jul-03 18:02
sussNguyen Van Tinh1-Jul-03 18:02 
GeneralRe: Superman Pin
HongMinhThi18-Sep-06 21:31
memberHongMinhThi18-Sep-06 21:31 
Generalfor() loop Pin
Anonymous30-Sep-02 0:39
sussAnonymous30-Sep-02 0:39 
GeneralHmm Pin
Liquid Sky8-Mar-02 20:01
memberLiquid Sky8-Mar-02 20:01 
GeneralCompiling Pin
Anonymous4-Nov-01 7:31
memberAnonymous4-Nov-01 7:31 
GeneralRe: Compiling Pin
Anonymous7-Jul-03 5:31
sussAnonymous7-Jul-03 5:31 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 2 Feb 2001
Article Copyright 2000 by Naveen K Kohli
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid