user9046719
user9046719

Reputation:

Ping: Remote device not able to ping but is connected

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

Answers (3)

Ravanan
Ravanan

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.

  • 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.

Upvotes: 1

Let's Enkindle
Let's Enkindle

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

Rainer Schaack
Rainer Schaack

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

Related Questions