• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1764
  • Last Modified:

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
0
ou81aswell
Asked:
ou81aswell
  • 3
  • 2
1 Solution
 
MedievalWarriorCommented:
The IPHostEntry will populate IPv6 address information as well so you can have multiple address entries in the AddressList. You can filter to return only IPv4 by checking the AddressFamily specifically Sockets.InterNetwork here is more information about AddressFamily enumeration http://msdn.microsoft.com/en-us/library/system.net.sockets.addressfamily.aspx
Dim entry As IPHostEntry = Dns.GetHostEntry(String.Empty)
        For Each ip As IPAddress In entry.AddressList
            ' Only display ipv4 address
            If ip.AddressFamily = Sockets.AddressFamily.InterNetwork Then
                Console.WriteLine(ip.ToString)
            End If
        Next

Open in new window

0
 
ou81aswellAuthor Commented:
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?
0
 
MedievalWarriorCommented:
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?
0
 
ou81aswellAuthor Commented:
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
0
 
ou81aswellAuthor Commented:
Thanks. Still not sure whic IP address to use. It appears that it's the first one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now