Marshal
Marshal

Reputation: 6671

Monitor continuosly if device is connected over IP

I have an electronic device which is connected to the computer over LAN. I have it's SDK to communicate with it.

Before communicating with the device it must be connected and registered with code provided in SDK. That's all fine.

My win application connects and registers the machine when the application starts up, but the problem is if the electronic device is SWITCHED OFF and then SWITCHED ON, the application must again connect and register the device.

So the question is how to continuously monitor if the device is connected over IP (something like PINGING it via C# code).

So one problem while constantly PINGing the device is that I must run it on separate thread, so that it doesn't affect my actual application. Another good solution would be if I get some piece code which fires event when the device is disconnected over IP.

Thanks for help. Regards,

EDIT 1: Some piece of code which I use to connect using SDK

bool connected;
connected = axCZKEM1.Connect_Net("192.168.1.201", "4370");

bool registered;
registered = axCZKEM1.RegEvent(1, 65535); 

EDIT 2: I am trying the method from answer by sa_ddam213, but I never get a ping failure. Also, it makes my PC run slow. What error am I making ?

while (true)
{
        Ping ping = new Ping();
        ping.PingCompleted += (sender, e) =>
        {
              if(e.Reply.Status != IPStatus.Success)
              {
                   connected=false;
                   registered=false;

                   while(connected && registered)
                   {
                       connected = axCZKEM1.Connect_Net("192.168.1.201", 4370);
                       registered = axCZKEM1.RegEvent(1, 65535);
                    }
               }
          };

          ping.Send("192.168.1.201", 3000);
}

Upvotes: 1

Views: 2917

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43626

You could Ping using c#,

 PingReply reply = new Ping().Send("127.0.0.1");
 if (reply.Status == IPStatus.Success)
 {
    // yay
 }

Async method:

Ping ping = new Ping();
ping.PingCompleted += (sender, e) => 
{
    if (e.Reply.Status == IPStatus.Success)
    {
        // yay
    }
};
ping.SendAsync("127.0.0.1", 3000, null);

Upvotes: 2

Related Questions