Click here to Skip to main content
6,629,377 members and growing! (20,494 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate

How To Get IP Address Of A Machine

By Naveen K Kohli

Tip on how to use DNS class and get IP address of a machine
C#, VC7, Windows, .NET 1.0, Visual Studio, Dev
Posted:30 Nov 2000
Updated:1 Feb 2001
Views:451,363
Bookmarked:66 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
85 votes for this article.
Popularity: 6.58 Rating: 3.41 out of 5
7 votes, 21.9%
1
2 votes, 6.3%
2
7 votes, 21.9%
3
7 votes, 21.9%
4
9 votes, 28.1%
5

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

About the Author

Naveen K Kohli


Member

Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 50 (Total in Forum: 50) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberJörgen Sigvardsson21:11 9 Jun '09  
GeneralRe: My vote of 1 PinmemberPRMan!!!9:26 24 Sep '09  
General[newbie] Pinmemberjon_805:05 29 Mar '09  
GeneralRe: [newbie] PinmemberByteBlocks8:27 29 Mar '09  
GeneralRe: [newbie] [modified] Pinmemberjon_8013:10 30 Mar '09  
GeneralRe: [newbie] PinmemberByteBlocks15:00 30 Mar '09  
Generalhello Pinmemberjr_jamesrobert0112:45 6 Jun '08  
Generalhow how u there Pinmemberjr_jamesrobert018:28 6 Jun '08  
GeneralWhat I'd really like to see is... Pinmembermycal9:24 27 Sep '07  
GeneralIPv6 (::1) issue with this code. Pinmemberdurkpurk2:03 28 Jul '07  
GeneralRe: IPv6 (::1) issue with this code. PinmemberMonir Sabbagh0:16 22 Apr '09  
Generalquestion in getting system information Pinmembervarghesh22:18 17 Mar '07  
GeneralGet all IPAddress in the network PinmemberWakwak Chimera20:25 3 Jan '07  
GeneralHow To Get IP Address & MAC address Of (Barcode Printer) of the Network? PinmemberJan Palmer19:46 3 Jan '07  
GeneralIP Collection PinmemberDragon Slayer2:47 8 Jul '06  
Questionclient/server on the same computer Pinmemberraniam20:38 25 Apr '06  
AnswerRe: client/server on the same computer Pinmemberpoduvajte9:11 21 Sep '07  
GeneralDNS should be Dns Pinmemberjinzhecheng6:14 8 Feb '06  
QuestionIP address related to a network connection type PinmemberPriyav0:39 30 Jan '06  
GeneralFramework 2.0 names Pinmemberrent0n0:56 17 Jan '06  
GeneralGet External IP PinmemberVitoto21:56 25 Jun '05  
Generalthanks! Pinmemberscottfm8:11 1 Apr '05  
GeneralHow can i see all IP address in local LAN PinmemberRattapol21:59 6 Jan '05  
GeneralRe: How can i see all IP address in local LAN PinsussMarcelo Amaolo8:26 21 Jan '05  
GeneralHow can i see all IP address in local LAN Pinmembersanjeev goyal7:02 20 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Feb 2001
Editor: Erik Thompson
Copyright 2000 by Naveen K Kohli
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project