Link to home
Create AccountLog in
Avatar of John
JohnFlag for Canada

asked on

Trying to get local IP address in VB 2010 - Can't get System.Net.Dns.GetHostEntry to work like System.Net.Dns.Resolve

After 2 hours of trying to do the simplest thing (finding the ip address of the local machine) and searching and searching but getting nowhere fast, I decided to ask my question here.

Apparently, System.Net.Dns.Resolve() is obsolete and I should use System.Net.Dns.GetHostEntry() instead.

I read that GetHostEntry() can simply be used instead of Resolve() as the paramaters and return types are the same but they're not.

Dim myHostName As String = System.Net.Dns.GetHostName()

Dim myIPAddress As String = System.Net.Dns.Resolve(myHostName).AddressList(0).ToString() returns "192.168.2.167" which is what I want.

BUT

Dim myIPAddress As String = System.Net.Dns.GetHostEntry(myHostName).AddressList(0).ToString() returns "fe80::112:6c34:8cc7:492d%10" which is Greek to me.

I gave up and left it using Resolve() but was hoping that someone could show me how to do it with a non-obsolete function.

Thanks

Environment: VB.Net / Visual Studio 2010 / Windows Forms Application on Win 7 x64
ASKER CERTIFIED SOLUTION
Avatar of MedievalWarrior
MedievalWarrior
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of John

ASKER

Thanks. I didn't realize the list contained different address families.

I'm confused about there being 3 addresses in my address list. Which IP address is the one that Internet Explorer uses to browse the web?
The IPAddress returned that looks like 192.168.2.167 is your computers network address usually obtained automatically from your DHCP server/router. May I ask what your real goal is because GetHostEntry() might not be what your looking for depending on what your trying to accomplish in your application. Could you elaborate?
Avatar of John

ASKER

I have a client app and a server app that use sockets to talk to each other across my LAN.

They talk together well.

I want to put the local IP address in the title bar of both apps
Avatar of John

ASKER

Thanks. Still not sure whic IP address to use. It appears that it's the first one.