|
-
Jan 15th, 2009, 07:18 AM
#1
Thread Starter
Lively Member
[2008] warning in my code for gethostbyname
Hi when i use the following code i get a warning
'Public Shared Function GetHostByName(hostName As String) As System.Net.IPHostEntry' is obsolete: 'GetHostByName is obsoleted for this type, please use GetHostEntry instead.
Code:
Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName) 'gets the machines current IP
now the code works and shows me the ip
-
Jan 15th, 2009, 07:21 AM
#2
Re: [2008] warning in my code for gethostbyname
 Originally Posted by smelf1111
now the code works and shows me the ip
So, what is your question?
-
Jan 15th, 2009, 07:28 AM
#3
Thread Starter
Lively Member
Re: [2008] warning in my code for gethostbyname
how to get rid of the warning, it asys use gethostentry but if i use that it displays the mac and not the ip
-
Jan 15th, 2009, 07:31 AM
#4
Re: [2008] warning in my code for gethostbyname
An obsolete method is one that still exists in the current version of the Framework but is intended to be removed at some later time. Given that the error message says:
please use GetHostEntry instead
I think it's fairly obvious what you should do. Don't you? In the next version of the Framework GetHostByName may be removed and your code will stop working. GetHostEntry is guaranteed to be available.
-
Jan 15th, 2009, 07:36 AM
#5
Re: [2008] warning in my code for gethostbyname
 Originally Posted by smelf1111
how to get rid of the warning, it asys use gethostentry but if i use that it displays the mac and not the ip
Well, it returns an IPHostEntry, which contains a property "AddressList" which is an array of IP addresses for the host.
-
Jan 15th, 2009, 07:42 AM
#6
Re: [2008] warning in my code for gethostbyname
 Originally Posted by smelf1111
how to get rid of the warning, it asys use gethostentry but if i use that it displays the mac and not the ip
That's not a MAC address. It's an IPv6 address. The IPAddress class has properties that tell you whether an IP address is IPv6 or IPv4.
-
Jan 15th, 2009, 07:52 AM
#7
Thread Starter
Lively Member
Re: [2008] warning in my code for gethostbyname
ah right so how do i just get it to display the ipv4 address?
-
Jan 15th, 2009, 08:02 AM
#8
Re: [2008] warning in my code for gethostbyname
 Originally Posted by jmcilhinney
That's not a MAC address. It's an IPv6 address. The IPAddress class has properties that tell you whether an IP address is IPv6 or IPv4.
Does it? I cant see such a property 
Or are you referring to IsIPv6LinkLocal and IsIPv6Multicast etc? in which case, would you have to test every one of those IsIPv6.. properties to ensure it wasnt a v6 address?
PS I know you could in theory just check to see if the string was a certain length as IPv6 addresses are longer but I'm curious to see if there is a more 'proper' way of doing it
-
Jan 15th, 2009, 08:54 AM
#9
Re: [2008] warning in my code for gethostbyname
The AddressFamily property will be Net.Sockets.AddressFamily.InterNetworkV6 if the the address is an IPv6 address.
-
Jan 15th, 2009, 08:59 AM
#10
Re: [2008] warning in my code for gethostbyname
Ahh that will be why I didnt spot it then, because microsoft decided that instead of naming it IPv4/v6 or something intuitive they called it InterNetwork and InterNetworkv6 lol thanks
-
Jan 15th, 2009, 10:35 AM
#11
Thread Starter
Lively Member
Re: [2008] warning in my code for gethostbyname
right i tried this
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myip As String = System.Net.Dns.GetHostAddresses(My.Computer.Name)(1).ToString().Trim
ComboBox1.Items.Add(myip)
End Sub
but it return fe80::5efe:192.168.0.19%10 i just wanted 192.168.0.19,
how do i get it to show that and not this fe80::5efe:192.168.0.19%10
-
Jan 15th, 2009, 10:59 AM
#12
Re: [2008] warning in my code for gethostbyname
Well why are you accessing the second item in the list of addresses? By using (1) in your code you refer to the second item in the array because the array is "zero based" (meaning the first item is item 0 and the second item is item 1 etc etc).
Also, I thought you were going to use GetHostEntry instead now?
One moment and I'll write an example to help you learn, not just to copy and paste and forget about
Last edited by chris128; Jan 15th, 2009 at 11:21 AM.
-
Jan 15th, 2009, 11:05 AM
#13
Re: [2008] warning in my code for gethostbyname
OK here we go:
vb Code:
'Loop through each IPAddress object in the list of IP addresses that is returned
'by the AddressList property
For Each IP As IPAddress In Dns.GetHostEntry(My.Computer.Name).AddressList
'Make sure its an IPv4 address (thanks Athiest!)
If IP.AddressFamily = Sockets.AddressFamily.InterNetwork Then
'Do something with the IP string, in this case show it in a messagebox
MessageBox.Show(IP.ToString)
End If
Next
-
Jan 15th, 2009, 11:05 AM
#14
Thread Starter
Lively Member
Re: [2008] warning in my code for gethostbyname
if you put a 0 it will only return the ipv6 address to get the ipv4 i use the 1,
chris your code returns 3 items for me
fe80::3894:6b0e:ed6c:7a1c%8
fe80::5efe:192.168.0.19%10
and
192.168.0.19
Last edited by smelf1111; Jan 15th, 2009 at 11:13 AM.
-
Jan 15th, 2009, 11:19 AM
#15
Re: [2008] warning in my code for gethostbyname
OK maybe the IF statement needs changing then to say NOT InterNetworkv6 instead of just IS InterNetwork
if you put a 0 it will only return the ipv6 address to get the ipv4 i use the 1
You said that your code returned the ipv6 address anyway, and you are using the 1 instead of the 0, so the above statement is not true is it 
In your situation using the 3rd item in the array (a (2) )would get you the v4 address because the v4 address is the third IP that is in your list of IP addresses... but that is only because your PC has 3 IP addresses, it would be different on other computers so you have to loop through them all and check instead of just grabbing one straight away like you are trying to do.
Last edited by chris128; Jan 15th, 2009 at 11:24 AM.
-
Jan 15th, 2009, 11:23 AM
#16
Thread Starter
Lively Member
Re: [2008] warning in my code for gethostbyname
cool, ipv6 must be on by default in vista.
-
Jan 15th, 2009, 11:25 AM
#17
Re: [2008] warning in my code for gethostbyname
Yeah it is
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|