GetHostEntry - How to handle host not found


  • Is there a way to handle host not found on a GetHostEntry lookup?

    Ex. My code below will return the IP of a machine, unless the hostname is not found, it then crashes.

    DIM sysMachineName, sysIPAddress AS String

    sysMachineName = Me.txtbx_Text1.Text

    sysIPAddress = System.Net.DNS.GetHostEntry(sysMachineName).Addresslist(0).ToString()

    Me.Text = "System Info For: " + sysMachineName + " - " & sysIPAddress

    Monday, March 20, 2017 5:06 PM

All replies

  • This code shows how System.Net.Dns.GetHostEntry should be used.  The Stops are there to show the error.  Remove to ignore the error or replace with your error handling code.

            Dim testnms() As String = {"", "", "", ""}
            For Each nm As String In testnms
                    Dim iphe As Net.IPHostEntry = Net.Dns.GetHostEntry(nm)
                Catch sex As Net.Sockets.SocketException
                    Stop 'hostname not found
                Catch ex As Exception
                    Stop 'other exceptions. see
                End Try

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Tuesday, March 21, 2017 11:18 AM
  • Hi Ryan,

    Not all IP's are setup properly with a reverse DNS entry. These IP's are typically end consumers on lazy ISP's who don't provide PTR records for their clients. If there's no reverse entry, there's no forward entry either. As such, these hosts have no hostname at all, hence the exception. You'll need to catch this exception for these hosts and use something else such as their IP as an identifier.

    Best Regards,

    Cherry Bu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, March 22, 2017 4:26 AM