有一个比较邪门的ping有关问题…
有一个比较邪门的ping问题……我的代码如下VB.NET codePrivate Sub SrvList_SelectedIndexChanged(ByVal sen
有一个比较邪门的ping问题…… 我的代码如下
VB.NET code Private Sub SrvList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SrvList.SelectedIndexChanged SrvGetInfo.Text = "测试连接……" If SrvList.Text = "启航(内测服务器)" Then If My.Computer.Network.IsAvailable Then If My.Computer.Network.Ping(mServerAdders) Then SrvGetInfo.ForeColor = Color.GreenYellow SrvGetInfo.Text = "服务器连接正常!" SrvSel.Enabled = True Else SrvGetInfo.ForeColor = Color.Red SrvGetInfo.Text = "服务器连接超时!" SrvSel.Enabled = False End If Else MsgBox("(-2)没有检测到网络环境!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly) End End If End If End Sub 我开启服务器的时候一切正常,但如果我把服务器关闭的话却出现了这个提示:
引用 有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾。 ************** 异常文本 ************** System.Net.NetworkInformation.PingException: Ping 请求期间发生异常。 ---> System.Net.Sockets.SocketException: 不知道这样的主机。 在 System.Net.Dns.GetAddrInfo(String name) 在 System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6) 在 System.Net.Dns.GetHostAddresses(String hostNameOrAddress) 在 System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options) --- 内部异常堆栈跟踪的结尾 --- 在 System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options) 在 System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer) 在 Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress, Int32 timeout) 在 SSWSS.SrvPage.SrvList_SelectedIndexChanged(Object sender, EventArgs e) 位置 E:\Projects\SSWSS\SSWSS\Form_StartScreen\SrvPage.vb:行号 38 在 System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e) 在 System.Windows.Forms.ComboBox.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** 已加载的程序集 ************** mscorlib 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll ---------------------------------------- Super Space Ship Wars - Side Story Episod I 程序集版本: 1.0.0.0 Win32 版本: 1.0.0.0 基本代码: file:///E:/Projects/SSWSS/SSWSS/bin/Debug/Super%20Space%20Ship%20Wars%20-%20Side%20Story%20Episod%20I.exe ---------------------------------------- Microsoft.VisualBasic 程序集版本: 8.0.0.0 Win32 版本: 8.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll ---------------------------------------- System 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- System.Windows.Forms 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System.Drawing 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System.Runtime.Remoting 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll ---------------------------------------- System.Configuration 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------- System.Xml 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll ---------------------------------------- System.resources 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.resources.dll ---------------------------------------- System.Windows.Forms.resources 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll ---------------------------------------- mscorlib.resources 程序集版本: 2.0.0.0 Win32 版本: 2.0.50727.1434 (REDBITS.050727-1400) 基本代码: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll ---------------------------------------- ************** JIT 调试 ************** 要启用实时(JIT)调试, 该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置 jitDebugging 值。 编译应用程序时还必须启用 调试。 例如: <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 启用 JIT 调试后,任何无法处理的异常 都将被发送到在此计算机上注册的 JIT 调试器, 而不是由此对话框处理。
我是想关闭服务器的时候能让它实现这个步骤……
VB.NET code SrvGetInfo.ForeColor = Color.Red SrvGetInfo.Text = "服务器连接超时!" SrvSel.Enabled = False 请问我该怎么做呢?
[解决办法] 需要使用try来捕捉异常!
[解决办法] 问题在于你在Ping一个网址的时候,它会先用Net.DNS.Resolve去找域名,找不到就会报错。
用Try...Catch ex as System.Net.NetworkInformation.PingException捕捉异常,并在Catch块中写SrvGetInfo.ForeColor = Color.Red
SrvGetInfo.Text = "服务器连接超时!"
SrvSel.Enabled = False
[解决办法] 捕捉异常,Catch时“服务器连接超时”