Pinging localhost [Answered]

The block below is a method that compiles without issue. However, when I try and pass it localhost:8888 it skips over the pingable = reply.Status == IPStatus.Success line into the catch. Am I passing the argument wrong or do I need a different method to handle local host?
public static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;

try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}

return pingable;
}
public static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;

try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}

return pingable;
}
9 Replies
#1AmericanDadFanForLife
it isn't a specific exception, as I haven't written one. It just returns false as that is the default value
Pobiega
Pobiega2y ago
well, try adding a catch(Exception ex) clause for now see if you are getting some other exception
#1AmericanDadFanForLife
{"An exception occurred during a Ping request."} + InnerException {"No such host is known"} System.Exception {System.Net.Sockets.SocketException}
Pobiega
Pobiega2y ago
there we go, and when an exception is thrown inside a try block, it jumps directly to the catch there is no guarantee that all lines in the try runs
#1AmericanDadFanForLife
System.Net.NetworkInformation.PingException: An exception occurred during a Ping request. ---> System.Net.Sockets.SocketException: No such host is known
at System.Net.Dns.GetAddrInfo(String name)
at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
--- End of inner exception stack trace ---
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress)
at CSharpcatcher.PingHost(String nameOrAddress) in C:\Users\alexander.noriega\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 22
System.Net.NetworkInformation.PingException: An exception occurred during a Ping request. ---> System.Net.Sockets.SocketException: No such host is known
at System.Net.Dns.GetAddrInfo(String name)
at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
--- End of inner exception stack trace ---
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress)
at CSharpcatcher.PingHost(String nameOrAddress) in C:\Users\alexander.noriega\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 22
here is the full block
Pobiega
Pobiega2y ago
yeah, pings are ICMP commands if I'm not mistaken as in, there is no port specified
#1AmericanDadFanForLife
I see. I suppose that closes this issue then. Thank you.
cap5lut
cap5lut2y ago
pls use the /close command to mark this thread as answered
Accord
Accord2y ago
✅ This post has been marked as answered!