mpf1748
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("localho st") and get the address, but that returns 127.0.0.1.
TIA,
Matt
TIA,
Matt
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.GetHostByNa me(System. Net.Dns.Ge tHostName)
Console.WriteLine(CType(h. AddressLis t.GetValue (0), IPAddress).ToString)
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.GetHostByNa
Console.WriteLine(CType(h.
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("L OCAL_ADDR" ).ToString
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("L
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
Matt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Imports System.Net
' Get host name
Dim strHostName As String = Dns.GetHostName()
' Find host by name
Dim iphostentry As IPHostEntry = Dns.GetHostByName(strHostN ame)
' 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
' Get host name
Dim strHostName As String = Dns.GetHostName()
' Find host by name
Dim iphostentry As IPHostEntry = Dns.GetHostByName(strHostN
' 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
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.GetHostByNa
oAddr = New System.Net.IPAddress(.Addr
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