Results 1 to 17 of 17

Thread: [2008] warning in my code for gethostbyname

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    125

    [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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] warning in my code for gethostbyname

    Quote Originally Posted by smelf1111
    now the code works and shows me the ip
    So, what is your question?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    125

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] warning in my code for gethostbyname

    Quote 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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] warning in my code for gethostbyname

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    125

    Re: [2008] warning in my code for gethostbyname

    ah right so how do i just get it to display the ipv4 address?

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] warning in my code for gethostbyname

    Quote 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    125

    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

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] warning in my code for gethostbyname

    OK here we go:
    vb Code:
    1. 'Loop through each IPAddress object in the list of IP addresses that is returned
    2. 'by the AddressList property
    3. For Each IP As IPAddress In Dns.GetHostEntry(My.Computer.Name).AddressList
    4.     'Make sure its an IPv4 address (thanks Athiest!)
    5.     If IP.AddressFamily = Sockets.AddressFamily.InterNetwork Then
    6.          'Do something with the IP string, in this case show it in a messagebox
    7.          MessageBox.Show(IP.ToString)
    8.     End If
    9. Next
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    125

    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.

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    125

    Re: [2008] warning in my code for gethostbyname

    cool, ipv6 must be on by default in vista.

  17. #17
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] warning in my code for gethostbyname

    Yeah it is
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width