Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB 2008 Error Getting IP Address 2

Status
Not open for further replies.

Christineeve

Programmer
Feb 12, 2005
104
US
Here is my code, the IP address is returned as a alpha instead of a number. Can anyone see what I might be doing wrong?

Code:
Private Sub GetIPAddress()

        Dim strHostName As String
        Dim myIPAddress

        strHostName = System.Net.Dns.GetHostName()
        myIPAddress = System.Net.IPAddress.Broadcast
        MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & myIPAddress)

    End Sub

Thank you for any help.
Christine
 
Not use GetHostName. The host name of a computer should be Alpha. I didn't even know about the Dns namespace. Looking at it I would assume what you want is System.Net.Dns.GetHostAddresses(). Then again looking at what you wrote again maybe not? Why whould you want the IP address twice or does that IPAddress.Broadcast not give you what you want? Scratch that do you know what you want? I guess try the GetHostAddresses and see if it is what you wanted and if not please explain in a little more depth.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
You need something more like this:
Code:
Dim strHostName As String
Dim strIPAddress As String

strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()

MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Hi,
Thanks for the help, it's much needed and appreciated. It was supposed to pull the local host and the IP address. I messed around with the code a lot and I see copied in my incorrect debugging code.
I will try the suggestions.
 
Thank you very much for the help. I got the value I needed in the correct format. My issue is resolved.

However, the intellisense returned a message stating that the line of code, "GetHostByName" is obsolete. Do you see harm in using it?:

Code:
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
Thanks much, I've provided stars!

Christine
 
It's obseleted....To be current, change that line to:
Code:
strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Hi,
Well so much for that. When I changed that code it went back to Alpha and not a normal IP Address. I'm going to use the your orginal suggestion.
 
You do have to be careful of that though. On a default Vista system (at least the ones I've tried), IPv6 is enabled, and that returns "::1".

You may have to cycle through the AddressList to find a non-loopback IPv4 address (assuming you are looking for IPv4 and not IPv6).
 
Dear Borvik,
Thanks for offering more info to me. I really don't know what you mean. Would you mind explaining the meaning of non-loopback 1Pv4.

I just need the IP address. Right now the code returns my router IP though. I'm guessing I really need the actual IP address that is leased to me (I don't have static IP).

Anything you might have to educate me would be great and appreciated.
Thank you!
Christine
 
Well, I just ran it collecting any and all IPs on my system, and I did not get 127.0.0.1. I was assuming it might as "::1" is the IPv6 loopback.

When I ran this bit of code:
Code:
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        Dim ips As New ArrayList()
        For i As Integer = 0 To h.AddressList.Length - 1
            ips.Add(CType(h.AddressList.GetValue(i), Net.IPAddress).ToString)
        Next

The ArrayList contained:
0: "::1"
1: "192.168.1.5"

However when I added the loopback check:
Code:
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        Dim ips As New ArrayList()
        For i As Integer = 0 To h.AddressList.Length - 1
            If Not Net.IPAddress.IsLoopback(CType(h.AddressList.GetValue(i), Net.IPAddress)) Then
                ips.Add(CType(h.AddressList.GetValue(i), Net.IPAddress).ToString)
            End If
        Next
I only ended up with the following:
0: "192.168.1.5"

Though this method will return ALL IP addresses on a system (if there is more than one).

As far as it returning your ROUTER IP address - it is possible I suppose (doing no research here so I'm just throwing out a theory) that if your computers full name (what is getting returned from GetHostName) is something like " and the DNS entry for that points to your router, then you may see something like that with this method.

Hope that helps.
 
Thank you for the great and educational post. Thanks for sharing your knowledge, it's much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top