.NET: Own IP Address Without Using Dns.

  • Thread starter Thread starter Neil B
  • Start date Start date
N

Neil B

Hi folks,

I need a way of deriving the IP address of the local machine (i.e. the
one running the process) without using the Dns class, since I cannot
guarantee the presence of a DNS in the final application. Any ideas?

Many thanks in advance for any help.

Neil B
 
Hello!
You wrote on Thu, 07 Apr 2005 19:28:33 +0100:

NB> I need a way of deriving the IP address of the local machine (i.e. the
NB> one running the process) without using the Dns class, since I cannot
NB> guarantee the presence of a DNS in the final application. Any ideas?

The IP address can't be obtained unless you have a connected socket, because
there can be more than one address on the computer, not counting 127.0.0.1.

With best regards,
Eugene Mayevski
 
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
foreach(IPAddress ip in ipHostInfo.AddressList)
{
Console.WriteLine(ip.ToString());
}

When Resolving the local host name returned from GetHostName(), the Resolver
does not use a query, but gets the local addresses without a network call.
So you don't need *any DNS server configured to do this as they are not used
anyway. Just remove your dns server ips to test it (and flush your local
cache with IPConfig /flushdns). HTH
 
Back
Top