Reputation: 2184
I have a list of server IP addresses and in part of my application, before sending a request to each server, I ping it. When I test it through VS 2012, part way through, it stops and displays a page in VS, saying "Unable to connect to the remote server". I have the ping in a try-catch but it doesn't fall into the exception section; it just dies executin ping.send(). Some of the other pings time out and I catch those and display proper message. When I check the exception trace (below), it says:"No connection could be made because the target machine actively refused it". Is there any way to catch and handle this rather stop the program from running?
This is (partial) ping code:
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);
This is the exception trace:
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:
When I manually ping this server (56.245.253.10), it replies.
Upvotes: 0
Views: 2308
Reputation: 3547
If you just want to test if the server is active by ping method, you can try this
private async Task<bool> PingHost(string ip)
{
bool pingable = true;
Ping mypinger = new Ping();
try
{
PingReply reply = mypinger.Send(ip);
pingable = (reply.Status == IPStatus.Success) ? true : false;
}
catch (PingException)
{
return false;
}
return pingable;
}
It runs asynchronously so doesn't block your main thread. You have to call it with await operator from another async method
Upvotes: 1