steve faleiro 1 Posted January 11, 2021 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
FPiette 385 Posted January 11, 2021 TWSocket is asynchronous (Means not blocking). Connect is just a request for TWSocket to do the connection. Connect returns long time before the actual connection is established. You really should read the documentation: http://wiki.overbyte.eu/wiki/index.php/Asynchronous_Paradigm You'd better start from an ICS example delivered in the distribution. Share this post Link to post
Angus Robertson 577 Posted January 11, 2021 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
FPiette 385 Posted January 11, 2021 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
Angus Robertson 577 Posted January 11, 2021 Quote But not all servers reply to ping request... www.msftncsi.com and ipv6.msftncsi.com both respond to ping. Angus Share this post Link to post
steve faleiro 1 Posted January 11, 2021 Thanks Francois and Angus for the quick responses. I will explore the samples and see what I can come up with. Share this post Link to post
Fr0sT.Brutal 900 Posted January 11, 2021 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
Angus Robertson 577 Posted January 26, 2021 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
steve faleiro 1 Posted January 26, 2021 (edited) 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 January 26, 2021 by steve faleiro Share this post Link to post