本文介绍了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
推荐答案
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-如何处理找不到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!