0

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;
            }
        }
  • 3
    are you sure the device is configured to respond to pings? Although in theory all devices on an IP network are supposed to respond, sometimes they don't, or the functionality can be disabled. Anyway, how would a ping report help you determine the cause of disconnection, exactly? – ADyson Jan 02 '18 at 16:43
  • @ADyson lets say for some reason, a device was not able to connect remotely and I need to check if it is being pinged or not. if it is being pinged but not connected that means the problem (with connection and data fetching) could be on my side, no? –  Jan 02 '18 at 17:02
  • And are you sure there are no devices (firewalls, routers, etc.) between that might be dropping ICMP packets? – Duston Jan 02 '18 at 17:40
  • @MickJagz it might mean that, or it might mean the device's software has crashed and isn't responding properly to requests. All a ping can really tell you is that the device is connected to the network. It's a basic network test. It doesn't tell you anything about its functional status. – ADyson Jan 02 '18 at 21:29

3 Answers3

1

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.

Rainer Schaack
  • 1,558
  • 13
  • 16
1

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;
        }
    }
Let's Enkindle
  • 57
  • 2
  • 17
1

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.

  • Check if your machine supports DHCP. If supports, enable it and restart the machine. Check if you can ping.
  • if above step is not working, disable DHCP in computer and the machine. set IP, subnet mask and default gateway manually in both computer and the attendance machine. Make sure subnet mask and default gateway are same in both, and first 3 parts of IP4 is same in the both. Still if you have problem, make sure the LAN wire to the computer and machine are coming from the same switch/hub.

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.

Ravanan
  • 536
  • 2
  • 13