Reputation:
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;
}
}
Upvotes: 0
Views: 2697
Reputation: 586
All the biometric machines are connected to the network are pingable. Before trying through the program, please check if you are able to ping the machine through standard ping command.
In general, for connecting the biometric machine successfully with your computer, try couple of steps.
Above steps will help you to connect the biometric machines with your computer. After the successful verification of ping, you can straight away call objZkeeper2.Connect_Net for connecting the machine from your program.
Upvotes: 1
Reputation: 53
please try with this sample code in my case it is working here I am passing some random string in your case, there is a chance it takes some garbage value and so it failed to ping device.
public static bool PingTheDevice(string ipAdd)
{
try
{
IPAddress ipAddress = IPAddress.Parse(ipAdd);
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "abcdefghijklmnopqrstuvwxyz";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(ipAddress, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
return true;
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}
Upvotes: 1
Reputation: 1618
Adding to ADyson's comment: yes, there may be devices which do not respond to ping. But I think you tested this already with ping.exe, didn't you ?
Your ping code itself works (just tested it). But: are you sure that ipAdd always contains a valid IP address and not a host name ? In the latter case, the Parse method throws an exception.
I had a look on some code from me, this part was from a diagnostics tool which runs in production for many years.
The code is:
public static bool Ping(string host)
{
for (int pass = 0; pass < 3; ++pass)
{
try
{
Ping pingSender = new Ping();
PingReply reply = pingSender.Send(host);
if (reply.Status == IPStatus.Success)
return true;
}
catch (Exception)
{
return false;
}
}
return false;
}
Instead of increasing the timeout, I use a retry loop. My experience is that a first ping may fail. It does not matter if you increase the timeout. So I had a better reliability using my own retry mechanism.
In addition, you can use it with a host name or IP address.
Upvotes: 1