You must first import/using System.Net
//To get a host with a www address
IPHostEntry ip = Dns.GetHostByName ("www.vbcity.com");
IPAddress [] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ());
}
//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 ());
}
---- VB
'To get a www address
Dim i As Integer
Dim ipE As IPHostEntry = Dns.GetHostByName("www.vbcity.com")
Dim IpA() As IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
Console.Write("IP Address {0}: {1} ", i, IpA(i).ToString)
Next
'To get local address
Dim sHostName As String
Dim i As Integer
sHostName = Dns.GetHostName()
Dim ipE As IPHostEntry = Dns.GetHostByName(sHostName)
Dim IpA() As IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
Console.Write("IP Address {0}: {1} ", i, IpA(i).ToString)
Next
You can use Dns.GetHostAddresses directly - fewer lines fewer bug possibilities. Good tip anyway solved a lot for me as I'm new to .NET TCP/IP. Thanks!
MAC-address follows:
Dim sMac As String
Dim Ni As NetworkInterface()
Ni = NetworkInterface.GetAllNetworkInterfaces ' get info
sMac = Ni(0).GetPhysicalAddress.ToString ' happend to be WLAN, 1 was LAN
sMac += Ni(0).Description.ToString() ' add node description
Debug.Print(sMac) ' - to sort out which node
|