Recent

Author Topic: How to detect internet connection!  (Read 15521 times)

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
How to detect internet connection!
« on: March 17, 2014, 01:09:21 pm »
Hi. How can I detect that the computer is not connected to the internet in an easy way?

if (are you connected to the internet) then begin

showmessage('You are not connected to the internet so, i have to quit. Try again when You have internet access.');

exit;
end;

Rob

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Paul Breneman

  • Sr. Member
  • ****
  • Posts: 290
    • Control Pascal
Re: How to detect internet connection!
« Reply #2 on: March 17, 2014, 02:03:26 pm »
Most of the stuff on this resource page was when we used to use modems to access the Internet, but there are some items (near the bottom of the page) you might find useful:
http://brenemanlabs.com/iConnect.htm
Regards,
Paul Breneman
www.ControlPascal.com

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: How to detect internet connection!
« Reply #3 on: March 18, 2014, 07:05:44 pm »
Most of the stuff on this resource page was when we used to use modems to access the Internet, but there are some items (near the bottom of the page) you might find useful:
http://brenemanlabs.com/iConnect.htm

If you use win95 and NT4 daily, maybe. Most of us are trying to kill off Windows XP before the end of the month though.

c4rlosC

  • Newbie
  • Posts: 1
Re: How to detect internet connection!
« Reply #4 on: May 18, 2019, 06:46:02 pm »
hello, someone knows how to program an internet connection detector in GNU / linux

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to detect internet connection!
« Reply #5 on: May 18, 2019, 07:58:57 pm »
I  can show you how to detect if google is online:

Code: Pascal  [Select][+][-]
  1. uses ...., netdb;
  2.  
  3. function HostByName (const who : string; var IP : String): Boolean;  inline;
  4. Var H : THostEntry;
  5.   begin
  6.     result := ResolveHostByName(who,H);
  7.     if result then IP := HostAddrToStr(H.Addr) else IP := '';
  8.   end;
  9.  
  10. procedure TestGoogle;
  11. var msg, IP : string;
  12. begin
  13. if HostByName ('www.google.de', IP ) then msg := 'Google online IP ='+IP else
  14.                                                                     msg := ' Google not online';
  15. showmessage (msg);
  16. end;
  17.  
  18.  
  19.  

Perhaps this is enough

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: How to detect internet connection!
« Reply #6 on: May 18, 2019, 08:08:07 pm »
http://ip.thaddy.com (online 99.8% since 1998) This is a  text string, not html...
Specialize a type, not a var.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: How to detect internet connection!
« Reply #7 on: May 18, 2019, 08:35:59 pm »
Quote
I  can show you how to detect if google is online:
I guess one website isn't enough, what if the google website is down or under attack or has got problems ...?
But this is the only way to check if an internet connection is possible. There is no piece of code that can do this without trying to get connected.

Normally it's not possible to detect if a internet connection ist possible. Even if I can connect to google right now, then there is no guaranty that it will work in 5 minutes or whenever ... depending on the security software the user uses.

Just in case there is some magician out there who can do it without establishing an internet connection, then PLEASE provide a link!  :D



Quote
http://ip.thaddy.com (online 99.8% since 1998) This is a  text string, not html...
Beautiful IP ... I love it  :P


Quote
Most of us are trying to kill off Windows XP before the end of the month though.
HERETIC  :P
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: How to detect internet connection!
« Reply #8 on: May 18, 2019, 11:08:01 pm »
Just in case there is some magician out there who can do it without establishing an internet connection, then PLEASE provide a link!  :D
Here is a way that doesn't require YOU to establish an internet connection but, that doesn't mean the system didn't establish one for you.

Read this article
https://www.codeproject.com/Tips/67577/Check-for-an-active-Internet-connection

The code is in C but, it's quite simple.  Porting it to Pascal should take very little time.

HTH.

PS: I haven't tried his method but, it looks reasonable and, seems to be along the lines of what you're asking for.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: How to detect internet connection!
« Reply #9 on: May 18, 2019, 11:23:17 pm »
Here is another example:
Code: Pascal  [Select][+][-]
  1. type
  2.   { TCheckInternet }
  3.   TCheckInternet = class(TFPHTTPClient)
  4.   public
  5.     class function Available(AServer: string='www.google.com'): boolean;
  6.   end;  // TCheckInternet
  7.  
  8.  
  9.  
  10. { TCheckInternet }
  11. class function TCheckInternet.Available(AServer: string): boolean;
  12. var
  13.   c: TCheckInternet;
  14. begin
  15.   Result := False;
  16.   c:=TCheckInternet.Create(nil);
  17.   try
  18.     try
  19.       c.ConnectToServer(AServer,80);
  20.       Result := True;
  21.     except
  22.     end;
  23.   finally
  24.     c.Free;
  25.   end;
  26. end;     // TCheckInternet.Available
  27.  
  28.  
  29.  
  30. function HasInternetConnection : boolean;
  31. begin
  32.   Result := TCheckInternet.Available();
  33. end;     // HasInternetConnection
  34.  
  35.  
  36.  
  37.  
  38. if HasInternetConnection then .....
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: How to detect internet connection!
« Reply #10 on: May 19, 2019, 04:43:17 am »
Quote
The code is in C but, it's quite simple.  Porting it to Pascal should take very little time.

Looks like everything for doing that is already on board...
Code: Pascal  [Select][+][-]
  1. unit JwaIpHlpApi;
  2.  
  3. function GetIpForwardTable(pIpForwardTable: PMIB_IPFORWARDTABLE; var pdwSize: ULONG;
  4.   bOrder: BOOL): DWORD; stdcall;
  5. {$EXTERNALSYM GetIpForwardTable}
I've never played around with this, thanks for the tip ...  :)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: How to detect internet connection!
« Reply #11 on: May 19, 2019, 11:33:03 am »
PS: I haven't tried his method but, it looks reasonable and, seems to be along the lines of what you're asking for.
Well... I have tried it some ions ago that's why I have this service. (Actually two, one is more advanced and detects redirects, see much older postings for context).
Boils down to: the only way to test for an internet connection is by making a connection to a reliable server.
In my case I give you your ip for free.... 8-) and as a string...not polluted by any html.... :P

Also... well... check my websites....Boring...
« Last Edit: May 19, 2019, 11:38:51 am by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: How to detect internet connection!
« Reply #12 on: May 19, 2019, 12:06:56 pm »
In my case I give you your ip for free.... 8-)
You're really funny.  Every internet connection you make has your ip.  So nice of you to give one's IP for free... a real Santa Ip you are. :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

metallaro1980

  • New Member
  • *
  • Posts: 36
Re: How to detect internet connection!
« Reply #13 on: May 19, 2019, 03:53:27 pm »
i am using mine

Code: Pascal  [Select][+][-]
  1. function TForm1.CheckInternet:boolean;
  2. var mypro1 : tprocess;
  3. var tmplst : tstringlist;
  4. var zk:integer;
  5. var tmpbuffer:string;
  6. var okbuffer:string;
  7. var artmp : array of string;
  8. var recev : integer;
  9. begin
  10.   try
  11.   mypro1 := tprocess.Create(nil);
  12.   tmplst := tstringlist.create;
  13.   mypro1.Executable:= 'ping';
  14.   mypro1.Parameters.Add('-c 5');
  15.   mypro1.parameters.add('8.8.8.8');
  16.   mypro1.Options := mypro1.Options + [poWaitOnExit, poUsePipes];
  17.   mypro1.Execute;
  18.   tmplst.loadfromstream(mypro1.output);
  19.   okbuffer := '';
  20.  
  21.   for zk := 0 to tmplst.Count - 1 do
  22.   begin
  23.     tmpbuffer := tmplst[zk];
  24.     if (pos('received', tmpbuffer) > 0) then
  25.     begin
  26.       okbuffer := tmpbuffer;
  27.       break;
  28.     end;
  29.   end;
  30.  
  31.   if (okbuffer = '') then
  32.   begin
  33.     result := false;
  34.     exit;
  35.   end
  36.   else
  37.   begin
  38.     tmpbuffer := '';
  39.     artmp := okbuffer.Split(',');
  40.     okbuffer := '';
  41.     for zk := 0 to length(artmp) - 1 do
  42.     begin
  43.       if (pos('received', artmp[zk]) > 0) then
  44.       begin
  45.         okbuffer := trim(artmp[zk]);
  46.         break;
  47.       end;
  48.     end;
  49.     if (okbuffer = '') then
  50.     begin
  51.       result := false;
  52.       exit;
  53.     end
  54.     else
  55.     begin
  56.        okbuffer := replacestr(okbuffer, 'received', '');
  57.        okbuffer := trim(okbuffer);
  58.        if isnumber(okbuffer) then
  59.        begin
  60.          recev := strtoint(okbuffer);
  61.          if (recev = 0) then
  62.          begin
  63.            result := false;
  64.            exit;
  65.          end
  66.          else
  67.          begin
  68.            result :=  true;
  69.            exit;
  70.          end;
  71.  
  72.        end
  73.        else
  74.        begin
  75.          result := false;
  76.          exit;
  77.        end;
  78.     end;
  79.  
  80.   end;
  81.  
  82.  
  83.   finally
  84.     mypro1.Free;
  85.   end;
  86.  
  87.  
  88. end;                          

here my source of my program to detect the internet ip in Lazarus
http://andreaverdi.altervista.org/linux/GetIP.zip

« Last Edit: May 19, 2019, 04:28:49 pm by metallaro1980 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: How to detect internet connection!
« Reply #14 on: May 19, 2019, 04:38:48 pm »
In my case I give you your ip for free.... 8-)
You're really funny.  Every internet connection you make has your ip.  So nice of you to give one's IP for free... a real Santa Ip you are. :)
No... external IP, and not even my direct IP. You are being silly. (or have not sufficient understanding how routing works)
Look up my original postings. Both links are still up after all those years. Also demonstrates the difference.  :-X :-X.... Plz take note... You deserve my mild insults...
And YES testing an internet connection can only be done through a full connection. Happens to be my job.. just in case...Anything else can be spoofed from a local network.
(Hence that site is fully static and doesn't need https)
« Last Edit: May 19, 2019, 04:47:34 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018