Jump to content
steve faleiro

How to check internet connection with TWSocket ?

Recommended Posts

In order to check if an internet connection is available, I am using TWSocket to connect to an internet server on port 80, but its always returning `False`. On debugging, I found that it never steps into the event handler.

 

I then tried with TSslWSocket (I've copied the Ssl DLLs into the EXE folder), and its the same thing.

 

Any thing wrong with this code?
 

type
  TDummy = class
    procedure wsocketOnSessionConnected(Sender: TObject; ErrCode: Word);
  end;

var
  lInternetConnected: Boolean;

procedure TDummy.wsocketOnSessionConnected(Sender: TObject; ErrCode: Word);
begin
  lInternetConnected := (ErrCode = 0);
end;

function CheckInternetConnection: boolean;
var
  wsocket: TSslWSocket;
  dummy: TDummy;
begin
  Result := False;

  dummy := TDummy.Create;
  try
    wsocket := TSslWSocket.Create(nil);
    try
      wsocket.Proto := 'TCP';
      wsocket.Port := '80';
      wsocket.Addr := 'www.google.com';
      wsocket.OnSessionConnected := dummy.wsocketOnSessionConnected;
      wsocket.Connect;
    finally
      wsocket.Free;
    end;

  finally
    dummy.Free;
  end;

  Result := lInternetConnected;
end;

 

Share this post


Link to post

Rather than accessing Google, you should check http://www.msftncsi.com/ncsi.txt and http://ipv6.msftncsi.com/ncsi.txt  which are the Microsoft Network Connectivity Status Indicator web pages used by almost all Windows installations every few seconds for many years to detect network connectivity for IPv4 and IPv6 (one or other may not work). They are designed for heavy use, and return a two word text page.

 

While you can use TCP to open a web page, there is a long timeout trying to connect, 30 seconds or more, so it's faster to use ICMP ping, to www.msftncsi.com, look at the OverbyteIcsPingTst.dpr sample, with ping you can set a five second (or less) timeout to get a quick response.

 

Angus

 

Share this post


Link to post
8 minutes ago, Angus Robertson said:

it's faster to use ICMP ping

But not all servers reply to ping request...

Share this post


Link to post
1 hour ago, Angus Robertson said:

While you can use TCP to open a web page, there is a long timeout trying to connect, 30 seconds or more, so it's faster to use ICMP ping, to www.msftncsi.com

It's not always an equivalent. In our network, pings are routed by gateway so any PC without internet is able to ping any remote host

Share this post


Link to post

I was updating the check alive capability of one of my applications to add IPv6 support this week, so created a new ICS component TIcsInetAlive to check for IPv4 and/or IPv6 internet
connectivity, using Ping and/or HTTP, defaulting to  www.msftconnecttest. com run by Microsoft for Windows 10 alive checking, online and offline check intervals may be set, event when online changes.   In SVN now.  There is a demo in OverbyteIcsHttpRestTst.dpr.

 

It is also a sample for using TSslHttpRest, needing only a few lines of code for HTTP requests. 

 

Angus

 

Share this post


Link to post
25 minutes ago, Angus Robertson said:

I was updating the check alive capability of one of my applications to add IPv6 support this week, so created a new ICS component TIcsInetAlive to check for IPv4 and/or IPv6 internet
connectivity, using Ping and/or HTTP, defaulting to  www.msftconnecttest. com run by Microsoft for Windows 10 alive checking, online and offline check intervals may be set, event when online changes.   In SVN now.  There is a demo in OverbyteIcsHttpRestTst.dpr.

 

It is also a sample for using TSslHttpRest, needing only a few lines of code for HTTP requests. 

 

Angus

 

Ok great. I will 'check' it out (no pun intended) 😉. Thanks!

Edited by steve faleiro

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×