GetHostEntry-如何处理找不到主机 [英] GetHostEntry - How to handle host not found

查看:74
本文介绍了GetHostEntry-如何处理找不到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以处理在GetHostEntry查找中找不到的主机?

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

例如我的下面代码将返回计算机的IP,除非未找到主机名,否则它将崩溃.

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

DIM sysMachineName,sysIPAddress AS字符串

DIM sysMachineName, sysIPAddress AS String

sysMachineName = Me.txtbx_Text1.Text

sysMachineName = Me.txtbx_Text1.Text

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

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

Me.Text =系统信息:" + sysMachineName +" -" & sysIPAddress

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

推荐答案

此代码显示应如何使用System.Net.Dns.GetHostEntry.停靠点在那里显示错误.删除以忽略该错误或替换为您的错误处理代码.

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 = {"", "foo.bar", "social.microsoft.com", "google.com"}
        For Each nm As String In testnms
            Try
                Dim iphe As Net.IPHostEntry = Net.Dns.GetHostEntry(nm)
                Debug.WriteLine(iphe.AddressList(0))
            Catch sex As Net.Sockets.SocketException
                Stop 'hostname not found
            Catch ex As Exception
                Stop 'other exceptions. see https://msdn.microsoft.com/en-us/library/ms143998(v=vs.110).aspx
            End Try
        Next


这篇关于GetHostEntry-如何处理找不到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆