Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Get the IP of a host

Getting the IP address and host name of a host / webhost using System.Net.

On Tuesday, June 15th 2004 at 11:14 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.5 with 11 votes)
Contextual Ads
More C# Resources
Advertisement
The following code asks you for a host and then returns the IP address of that host. The code is self-explanatory thanks to the comments.





using System;

// using the System.Net namespace

using System.Net;

class Class1

{

[STAThread]

static void Main(string[] args)

{

// Request the name

Console.Write("Please type the name of the host: ");

// Store it in 'host'

string host = Console.ReadLine();

try

{

// Get the DNS information

IPHostEntry ipHost = Dns.GetHostByName(host);

// Display the host name

Console.WriteLine("Host name: {0}", ipHost.HostName);

// Store the list of IP adresses

IPAddress[] ipAddr = ipHost.AddressList;

// Loop to actually display the IP

for(int x = 0; x < ipAddr.Length; x++)

{

Console.WriteLine("IP address: {0}", ipAddr[x].ToString());

}

}

// Catch the exception (if host was not found)

catch(System.Net.Sockets.SocketException)

{

Console.WriteLine("Host not found.");

}

}

}



Here is an example result:





Please type the name of the host: www.geekpedia.com

Host name: geekpedia.com

IP address: 65.75.151.74

Press any key to continue



Also if you want to return the hostname of an IP use Dns.Resolve() instead of Dns.GetHostByName.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Andy Jump on Thursday, July 8th 2004 at 05:06 AM

ther reverse can be done very easily too by just exchanging the

Dns.GetHostByName(host);

for

Dns.GetHostByAddress(IP);

this will only work if the dns entry has a reverse lookup though.

by ali on Monday, April 18th 2005 at 06:01 PM

i want information about code get ip header in c#

by Attila Turóczy on Saturday, July 22nd 2006 at 12:36 AM

Perfect tutorial!!

by herumi on Thursday, December 13th 2007 at 05:28 AM

Why the application close automatically before I see the result???
Thanks for share this tutorial???

by on Thursday, January 3rd 2008 at 01:43 PM

"Why the application close automatically before I see the result???
Thanks for share this tutorial???"

Because it has finished it's job. Add Console.Readline after "try".

by Rosen Rusev on Tuesday, January 8th 2008 at 11:10 AM

Simple but effective!

by genf20 reviews on Sunday, August 14th 2011 at 05:48 PM

Why is there a need to get a host's IP? I still don;t get it.

by Latoria on Saturday, August 20th 2011 at 07:00 AM

also you can open CMD and write this text :
tracert [your URL]
it is much easyer ;)

by keyboard for the ipad on Saturday, October 22nd 2011 at 11:55 AM

Is this guide is safe and secured, this is great if there is no problem on the host system.

by Replica watches on Monday, November 21st 2011 at 12:39 AM

on the other hand belong to a category which at best can be called cheap and unworthy. They never give you your desired level of satisfaction. A fake watch is never an exact replacement of a designer or branded watch.

by online magazines on Monday, December 12th 2011 at 04:59 AM

Looks like you don't have permission over the Insert command. Check the user's credentials.and plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.

by michael kors bags on Sunday, March 18th 2012 at 08:59 PM

Thank you for sharing, I will often come to patronize your site


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia
Other Resources
Bargain EZ Tech Coupons & Deals