Mike at Bookup
Mike at Bookup

Reputation: 1314

Detect internet access in Delphi

My tethering app sometimes does not get internet access. In these cases Windows will not ask whether the app has permission to use the internet. My app generates no errors but of course will not tether.

How can I test whether my Delphi app has access to the internet sufficient for tethering?

Upvotes: 4

Views: 3798

Answers (1)

Olaf Hess
Olaf Hess

Reputation: 1483

The following code should work on all platforms:

uses IdTCPClient;

function CheckInternet : Boolean;

var TCPClient : TIdTCPClient;

begin
  TCPClient := TIdTCPClient.Create (NIL);

  try
    try
      TCPClient.ReadTimeout := 2000;
      TCPClient.ConnectTimeout := 2000;
      TCPClient.Port := 80;
      TCPClient.Host := 'google.com';
      TCPClient.Connect;
      TCPClient.Disconnect;
      Result := true;

      except
        Result := false;
      end; { try / except }

    finally
      TCPClient.Free;
    end; { try / finally }
end;

Source: www.fmxexpress.com

A library to check for an Internet connection on mobile devices can be found at www.delphiworlds.com

Upvotes: 7

Related Questions