Paul Fryer
Paul Fryer

Reputation: 9527

How to get IP address of server in .NET?

How do you get the IP address of the web/application server in .NET? Not the client IP address, but the server IP address.


I just found something about server variables.

Upvotes: 0

Views: 2002

Answers (4)

ankitjaininfo
ankitjaininfo

Reputation: 12362

To get the IP address (and Coutnry/location) of a server programatically I use Utrace.de API. It returns an XML with IP address and location information too.

Example query: http://xml.utrace.de/?query=google.com

Upvotes: -2

ankitjaininfo
ankitjaininfo

Reputation: 12362

To get my own IP address in C#

IPHostEntry ipEntry = DNS.GetHostByName (Dns.GetHostName());
IPAddress [] addr = ipEntry.AddressList;

To get for other's machine

IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;

Upvotes: 1

NeilDurant
NeilDurant

Reputation: 2132

You probably want something like the following code, to get all IP addresses of the current machine. However it won't tell you which network adaptor (and thus IP address) a particular request came in on, if you have more than one.

   String strHostName = Dns.GetHostName();
   Console.WriteLine("Host Name: " + strHostName);

   // Find host by name
   IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

   // Enumerate IP addresses
   foreach(IPAddress ipaddress in iphostentry.AddressList)
   {
       Console.WriteLine(ipaddress.ToString());
   }

Upvotes: 3

McKay
McKay

Reputation: 12614

There are services that tell you what your external IP address is, but it might be subject to change.

Sample Services:

Upvotes: 0

Related Questions