#1AmericanDadFanForLife
#1AmericanDadFanForLife
CC#
Created by #1AmericanDadFanForLife on 1/17/2023 in #help
✅ How to make Event and Delegate to Subscribe to the LocalHost
I am trying to make a c# copy of an existing java program. The individual items I need to capture are clear, but I lack the C# sharp knowledge needed to actually get the program to listen to the local host. My apologies if the problem listed here is vague, but I am lacking some basic knowledge myself.
19 replies
CC#
Created by #1AmericanDadFanForLife on 11/21/2022 in #help
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;
}
15 replies