【发布时间】:2014-11-14 19:07:23
【问题描述】:
我有一个服务器 IP 地址列表,在我的应用程序的一部分中,在向每个服务器发送请求之前,我会对其进行 ping 操作。当我通过 VS 2012 对其进行测试时,中途停止并在 VS 中显示一个页面,显示“无法连接到远程服务器”。我在 try-catch 中有 ping,但它不属于异常部分;它只是在执行 ping.send() 时死掉。其他一些 ping 超时,我捕捉到这些并显示正确的消息。当我检查异常跟踪(如下)时,它说:“无法建立连接,因为目标机器主动拒绝它”。有什么办法可以捕捉和处理这个而不是阻止程序运行?
这是(部分)ping 码:
public static string PingServer(string host)
{
PingOptions options = new PingOptions();
options.DontFragment = true;
Ping p = new Ping();
PingReply reply = null;
string NetworkMessage = string.Empty;
try
{
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
Console.WriteLine(host);
reply = p.Send(host, timeout, buffer, options);
这是异常跟踪:
System.Net.WebException was unhandled
HResult=-2146233079
Message=Unable to connect to the remote server
Source=System
StackTrace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at PASSCrawlerNS.PASSCrawler.GetResponseCallback(IAsyncResult ar) in c:\Users\blah\Documents\Visual Studio 2012\Projects\test\test\Program.cs:line 102
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.HttpWebRequest.SetResponse(Exception E)
at System.Net.ConnectionReturnResult.SetResponses(ConnectionReturnResult returnResult)
at System.Net.Connection.CompleteConnectionWrapper(Object request, Object state)
at System.Net.PooledStream.ConnectionCallback(Object owningObject, Exception e, Socket socket, IPAddress address)
at System.Net.ServicePoint.ConnectSocketCallback(IAsyncResult asyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.Sockets.Socket.ConnectCallback()
at System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object state, Boolean timedOut)
InnerException: System.Net.Sockets.SocketException
HResult=-2147467259
**Message=No connection could be made because the target machine actively refused it** 56.245.253.10:80
Source=System
ErrorCode=10061
NativeErrorCode=10061
StackTrace:
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
InnerException:
当我手动 ping 此服务器 (56.245.253.10) 时,它会回复。
【问题讨论】:
-
你捕获的异常是什么?这是在调试期间吗?如果按 F5/继续调试会发生什么?
-
您已经在该代码周围有一个
try,那么catch是什么样的?此外,您可以按Ctrl+Alt+E并选中/取消选中“公共语言运行时异常”下的“抛出”,让调试器提供更多/更少有关程序异常的信息。 -
一个更简单的问题:当 VS 由于异常而停止您的程序时,它会突出显示该行。它是什么颜色?如果它是黄色的,则表示您没有处理异常。如果它是绿色的,它将被处理,您可以按 F5 继续运行。
-
为什么要ping美国邮政服务范围内的IP?为什么堆栈跟踪中有一个叫做 PASSCrawler 的东西?
-
实际上我包含的跟踪包含异常。它说:“消息=无法建立连接,因为目标机器主动拒绝了它 56.245.253.10:80”。我可以从控制台 ping 它,它会回复。我没有包括 catch 部分的原因是因为它不属于它。它尝试执行 ping.send() 并死在那里。