I have a remote device (biometric in my case) placed at a location X. while being at location Y I am able to connect to that device. However I need to know the cause everytime the connection fails or is unable to connect during the day. For that, I am trying to ping the device each time it is connected or the connection is not established, so I would know the cause of disconnection. The snippet below returns false, i.e. Not able to ping even when the device is able to connect successfully. I tried to increase the timeout
but it still shows timed out and eventually failed to ping.
Controller:
bool isPinged = false;
isConnected = objZkeeper2.Connect_Net(IpAddr, Port);
if (isConnected)
{
//considering the connection is established, I am sending a ping req
isPinged = PingTheDevice(IpAddr);
Then:
public static bool PingTheDevice(string ipAdd)
{
try
{
IPAddress ipAddress = IPAddress.Parse(ipAdd);
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
byte[] buffer = new byte[32];
int timeout = 5000;
PingReply reply = pingSender.Send(ipAddress, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
return true;
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}