Atozed Forums
detect lost internet connection via a hook - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Indy (https://www.atozed.com/forums/forum-8.html)
+--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html)
+--- Thread: detect lost internet connection via a hook (/thread-2702.html)



detect lost internet connection via a hook - Ahmed Sayed - 04-06-2022

Hi,
I am in the process of creating a push notification service where the clients will be connected all the time to the http server providing the service. I am creating the server and client apps, but I need to detect when the connection is lost on the client side via something similar to a windows hook. I don't want to create a TTimer or a thread to keep checking the connection. I want something more smarter that mimic the same thing on cell phones (Android/iOS).

I want the client to know when the connection was lost and when it was restored so it can reconnect to the service again automatically. 

Knowing that the PCs that will run this will be inside a domain so some users will be allowed access to the internet, some won't. So I can't do something like keep pinging "google.com" or any other website because it will not work for some users. 

This has to be done using Indy components only because I want the app to support other OS(s) as well when the C++ Builder supports Linux for example. So it has to be as cross platform as possible.

For example, I want the program to detect if I removed the Ethernet cable or turned off the WiFi on the laptop, and when I reconnect the Ethernet cable or turn WiFi on it should detect that too.

I remember there was a talk about this or something similar somewhere but I can't find it again. Anyway

Thanks in advance,
Ahmed Sayed


RE: detect lost internet connection via a hook - rlebeau - 04-06-2022

(04-06-2022, 08:30 AM)Ahmed Sayed Wrote: For example, I want the program to detect if I removed the Ethernet cable or turned off the WiFi on the laptop, and when I reconnect the Ethernet cable or turn WiFi on it should detect that too.

That is outside of Indy's scope. There is no cross-platform way to accomplish what you are asking for. You need to resort to platform-specific APIs. For instance, the Network List Manager API on Windows, the ConnectivityManager API on Android, etc.


RE: detect lost internet connection via a hook - kudzu - 04-07-2022

Remy is correct, however you can do a bit of this with Indy but not with all the details. Most routers use a ping method, they simply ping a known server using ping protocol, or UDP or HTTP to a known server to see if a connection exists or not. You can do that with Indy.


RE: detect lost internet connection via a hook - rlebeau - 04-07-2022

(04-07-2022, 09:12 PM)kudzu Wrote: Most routers use a ping method, they simply ping a known server using ping protocol, or UDP or HTTP to a known server to see if a connection exists or not. You can do that with Indy.

I wouldn't even bother doing that much. Since the client needs to establish a connection to receive the push notifications, I would suggest simply having the client connect, send a request, read in a loop until disconnected, and reconnect and repeat if needed. If the network connection is lost, let the OS close the connection, which will fail any current/subsequent socket operation until the socket is closed. You can then catch those errors and move on. This is very easy to implement with Indy, especially in a worker thread (which you MUST use on Android anyway, as you CAN'T perform network operations on the main UI thread), eg:

Code:
while not Terminated do
begin
  try
    // connect...
  except
    // error handling...
    sleep(5000);
    Continue;
  end;

  try
    try
      // send request to begin push notifications...
      while not Terminated do
      begin
        // read and process a push notification...
      end;
    except
      // error handling...
    end;
  finally
    // disconnect...
  end;
end;

This way, the client doesn't have to care WHY the connection is lost, or WHY it is unable to (re-)connect. It just keeps trying until successful.

But, if you REALLY want to include a network status check, this approach allows you to do that, eg:

Code:
while not Terminated do
begin
  try
    if (network is not OK) then
    begin
      sleep(5000);
      Continue;
    end;
    // connect...
  except
    // error handling...
    sleep(5000);
    Continue;
  end;

  // use the connect as needed...
end;

For instance, you could have another thread that responds to OS notifications about network status changes. Or, on OSes that don't offer that, then simply poll the network status periodically. When you detect a valid network, set a boolean, or signal a TEvent, etc. When you detect a bad network, clear the boolean, or reset the TEvent, etc.

Then have the socket thread check/wait-on a valid network to be detected before attempting to open its socket connection.