Link to home
Start Free TrialLog in
Avatar of mpf1748
mpf1748Flag for United States of America

asked on

Get IPAddress of machine

Is there a way to obtain the IPAddress of the local machine (in C#/C++) without relying on the result of Dns.GetHostName()? The reason I ask is because I have run into issues obtaining the HostName of a machine, and also, the method can throw a SocketException. I have tried to create an IPHostEntry with Dns.GetHostByName("localhost") and get the address, but that returns 127.0.0.1.

TIA,
Matt

Avatar of Mikal613
Mikal613
Flag of United States of America image

One of the many namespaces in the Base Class Framework is the System.Net namespace. It provides a simple programming interface to many of the protocols found on the network today. One of the classes of the namespace is DNS which provides simple domain name resolution functionality.

The following example demonstrates the use of System.NET.DNS namespace to get the computer Name and it’s IP address.
 
Option Strict Off
Imports system
Imports System.Net.DNS
  Public Class GetIP

    Shared Function GetIPAddress() As String
      Dim oAddr As System.Net.IPAddress
      Dim sAddr As String
      With system.Net.DNS.GetHostByName(system.Net.DNS.GetHostName())
         oAddr = New System.Net.IPAddress(.AddressList(0).Address)
         sAddr = oAddr.ToString
      End With
      GetIPAddress = sAddr
    End Function

    Shared Sub main()
      Dim shostname As String
      shostname = system.Net.DNS.GetHostName
      console.writeline("Your Machine Name = " & shostname)
      'Call Get IPAddress
      console.writeline("Your IP = " & GetIPAddress)
    End Sub

 End Class
or

To find your IP address we first ask for your machine name, then we pass that to the GetHostName function that gives us a valid IPHostEntry object. This object contains your IP address in the AddressList array, so we just dump the value to the console.

 Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
 Console.WriteLine(CType(h.AddressList.GetValue(0), IPAddress).ToString)

or
1. C#:
//from http://www.devcity.net/net/article.aspx?alias=gethostbyname
//To get the local IP address
string sHostName = Dns.GetHostName ();
IPHostEntry ipE = Dns.GetHostByName (sHostName);
IPAddress [] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ());
}


2: Old fashion ASP (VB.NET)
Dim sIP As String = Request.ServerVariables("LOCAL_ADDR").ToString
Avatar of mpf1748

ASKER

Yes, I can get the IPAddress with Dns.GetHostName(), but I need to be able to get the IPAddress _without_ using the result of Dns.GetHostName(). I would either like an answer or be told that it can't be done. Also, I am not using ASP or ASP.NET so the second solution of Mikal613's post will not work for me.

Matt
ASKER CERTIFIED SOLUTION
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DotNetDoc
DotNetDoc

Imports System.Net


' Get host name
        Dim strHostName As String = Dns.GetHostName()

        ' Find host by name
        Dim iphostentry As IPHostEntry = Dns.GetHostByName(strHostName)

        ' Enumerate IP addresses
        Dim nIP As Integer = 0
        Dim ipaddress As IPAddress
        For Each ipaddress In iphostentry.AddressList
            MessageBox.Show(("IP #: " & ipaddress.ToString()))
           
        Next ipaddress