Recent

Author Topic: is there an internet connection?  (Read 3106 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2363
    • tomboy-ng, a rewrite of the classic Tomboy
Re: is there an internet connection?
« Reply #30 on: February 04, 2023, 11:51:49 pm »
I have to say that pinging google is a pretty reliable solution.

Many systems stopped responding to the "mandatory ECHO_REQUEST" (to quote my man page) when the Ping of Death became a big thing. Since then the internet has learnt to deal with a lot more elaborate DOS attacks and just pinging someone does not really cut it. So, again, a good number of systems do support ping again.

From memory, and I cannot find a reference, google did announce that they intended to always respond to ping except in exceptional, short term circumstances. (It may have been at a conference ?)

I always ping google.com, that way, as well as connectivity, I ensure I also have DNS.

So, yes, there is theoretical argument that maybe the net is OK but someone has accidentally knocked the power lead out of the googles server but I think you are pretty safe.  I'm more worried about cosmic radiation flipping ram bits than google being offline.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Nicole

  • Hero Member
  • *****
  • Posts: 731
Re: is there an internet connection?
« Reply #31 on: February 08, 2023, 07:18:11 pm »
Paweld, thanks for the project.
Unfortunately I have huge problems with it (Laz. 2.2.4).

The IDE shutw down or gives me error-messages on displaying the form by F12 after a few seconds.
Another attempt made Lazarus "just disappear".

Let me put it like this.
Can somebody help me to write such a method

function myPing: Boolean;

begin
  result:=false;

  ping 4.2.2.1;  // no idea, how to send
  GetAnswertime;  // no idea, how I can get it
  If answertime > 0 and answertime < 300  // or a similar check for "there is some internet"
    then result:=true;

end;
 

JanRoza

  • Hero Member
  • *****
  • Posts: 656
    • http://www.silentwings.nl
Re: is there an internet connection?
« Reply #32 on: February 08, 2023, 07:51:50 pm »
Quote
ping 4.2.2.1;  // no idea, how to send 

Of course you know how, AF0815 already gave you an answer with code in which you can see how to ping.
What’s the use of examples if you don’t study them?
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
Laz: Lazarus 2.2.6 FPC 3.2.2
       CodeTyphon 8.10 FPC 3.3.1

af0815

  • Hero Member
  • *****
  • Posts: 1166
Re: is there an internet connection?
« Reply #33 on: February 08, 2023, 07:54:12 pm »
and you have two timeouts in the sample. With this you can deal with the answertime. Your version of Lazarus is meaningless, the sample works for years and on win32, win64, linux-x64, linux-arm,...
« Last Edit: February 08, 2023, 07:56:04 pm by af0815 »
regards
Andreas

Curt Carpenter

  • Full Member
  • ***
  • Posts: 226
Re: is there an internet connection?
« Reply #34 on: February 08, 2023, 09:30:28 pm »
You will conquer it Nicole!

Can you use the Online Package Manager to install the Synapse 40.1 package on your computer? 

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1172
  • Fifty shades of code.
    • Delphi & FreePascal
Re: is there an internet connection?
« Reply #35 on: February 08, 2023, 10:20:24 pm »
To me, the only reliable way is to send or get real data.
Do it like Microsoft do it since ages.
I already read, you do not want to learn how things are working, you just want to copy and paste something what others made.
So here copy this and place it in a unit which uses WinINet.
Code: Pascal  [Select][+][-]
  1. function CheckMicrosoftConnectivity(const SuppressErrors: Boolean = False): Boolean;
  2. var
  3.   NetHandle: HINTERNET;
  4.   UrlHandle: HINTERNET;
  5.   Buffer: array[0..1023] of Byte;
  6.   BytesRead: DWord;
  7.   StrBuffer: UTF8String;
  8.   Received: string;
  9. begin
  10.   Result := False;
  11.   Received := '';
  12.   StrBuffer := '';
  13.   BytesRead := Default(DWord);
  14.   NetHandle := InternetOpen('Mozilla/5.0(compatible; WinInet)', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  15.   if Assigned(NetHandle) then
  16.     try
  17.       UrlHandle := InternetOpenUrl(NetHandle, PChar('http://www.msftncsi.com/ncsi.txt'), nil, 0, INTERNET_FLAG_RELOAD, 0);
  18.       if Assigned(UrlHandle) then
  19.         try
  20.           repeat
  21.             if InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead) then
  22.               SetString(StrBuffer, PAnsiChar(@Buffer[0]), BytesRead);
  23.             Received := Received + StrBuffer;
  24.           until BytesRead = 0;
  25.           Result := (Received = 'Microsoft NCSI');
  26.         finally
  27.           InternetCloseHandle(UrlHandle);
  28.         end
  29.       else
  30.         if not SuppressErrors then
  31.           ShowMessage('Cannot open URL: www.msftncsi.com');
  32.     finally
  33.       InternetCloseHandle(NetHandle);
  34.     end
  35.   else
  36.     if not SuppressErrors then
  37.       ShowMessage('Unable to initialize WinInet');
  38. end;
It does not require any kind of SSL library.

If you find it useful and want to learn, Microsoft has all the answers.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Joanna

  • Sr. Member
  • ****
  • Posts: 293
Re: is there an internet connection?
« Reply #36 on: February 09, 2023, 02:28:00 am »
Here is a way to detect internet connection given to me awhile ago by someone in IRC.

uses pingsend file of laz_synapse unit

myPINGSEND:TPINGSend; 

FUNCTION CAN_CONNECT( CONST URL: STRING) : BOOLEAN;
 BEGIN
 try
    RESULT:= myPingSend.Ping(URL);
   finally
   end;
 END;

I_have_internet:= CAN_CONNECT('1.1.1.1');
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
Private Message me if you want to be added to invite list, otherwise type /msg nickserv help register to get started

Nicole

  • Hero Member
  • *****
  • Posts: 731
Re: is there an internet connection?
« Reply #37 on: February 15, 2023, 10:16:03 am »
@KodeZwerg
Finally the output window is solved, did not expect it to take that long.
Next task: ping, thank you so much for the demo.

This is my problem
IdIcmpClient
of the uses list is not found.

What to change?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1172
  • Fifty shades of code.
    • Delphi & FreePascal
Re: is there an internet connection?
« Reply #38 on: February 15, 2023, 10:46:26 am »
What to change?
With my demo you do not need Ping or Indy or Ssl or admin rights to execute or whatever else bad reasons exists to not use Ping.
You only need to include WinINet, that is already a basic unit when you install Lazarus, and get a result of my method, so I do not really understand your question at all.

I am happy that you fixed some of the problems you are into.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

TRon

  • Hero Member
  • *****
  • Posts: 834
Re: is there an internet connection?
« Reply #39 on: February 15, 2023, 11:13:05 am »
@KodeZwerg:
I believe the remark you quoted from TS was addressed at the wrong person, hence your confusion ?

It seems to me the project from paweld in post #16 uses indy.

@Nicole:
If you are indeed referring to paweld's project then you need to install the Indy components to solve the compiler error. It can't find the unit because you do not seem to have indy installed.

Unfortunately I can't give advise on how to install indy as the last time I tried to do that in Lazarus (Linux) it failed for me.
« Last Edit: February 15, 2023, 11:22:09 am by TRon »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1172
  • Fifty shades of code.
    • Delphi & FreePascal
Re: is there an internet connection?
« Reply #40 on: February 15, 2023, 11:38:18 am »
@KodeZwerg:
I believe the remark you quoted from TS was addressed at the wrong person, hence your confusion ?

It seems to me the project from paweld in post #16 uses indy.
That makes sense  :D

Okey, rewind, download this file: NicolesProject.zip
Extract its content into a new folder of your choice.
Open Lazarus and click on open project.
Change filter from *.lpi to *.* and select project1.lpr file, when asked to create a new project for it, say yes and choose application (lcl).
Click now the menu project options and choose project inspector.
Now click on the small down arrow at the "Add" and choose "New Requirement" (Image 1)
Select Indy (Image 2) and press "OK"
Now you can close the Project Inspector and project will compile.

If you not have Indy installed, you can do it with the OPM utility very easy (Image 3)

Are questions left?

« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10577
  • FPC developer.
Re: is there an internet connection?
« Reply #41 on: February 15, 2023, 11:40:20 am »
Kodezwerg: It might we worthwhile to arrange that example to fit into winutils (an unit for small winapi calls).  Do you know if this can fail in case of proxy misconfiguration?

icmp direct use afaik indeed requires admin rights.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1172
  • Fifty shades of code.
    • Delphi & FreePascal
Re: is there an internet connection?
« Reply #42 on: February 15, 2023, 11:51:17 am »
Do you know if this can fail in case of proxy misconfiguration?
I do use InternetOpen with INTERNET_OPEN_TYPE_PRECONFIG flag.
Quote
Retrieves the proxy or direct configuration from the registry.
I guess when your computer has a corrupt configuration that it fail but I never tested it under such condition (!!)
I am sorry that I neither can say clearly yes or no.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Nicole

  • Hero Member
  • *****
  • Posts: 731
Re: is there an internet connection?
« Reply #43 on: February 15, 2023, 09:11:56 pm »
@KodeZwerg: Thank you for your patience.
I implemented your function now and for the first time the job is done without problems, without error-message, without strange unit.

Now, you will ask: "Why did she hesitate to do so for one moment?"

Well, there are reasons, 2 of them:

1)
Once upon a time there was a rainy day, I did nothing special at all, nothing with the net, nothing with software, no browser, really nothing.
On this day my DNS-resolving was shot. It took me a while to find out about my connection problem at all.
My Win 7 ate my DNS-resolutioin. Somehow I repaired them. Long ago, I cannot remember any more how. My backup shall hold a file about.

To sum up: I rather would prefer a check without DNS-resolve needed.

2)
It is Microsoft. Those hated-by-me-guys have the best operating system and I use the stuff of them all they long, because it is grrr good. Never the less, I would prefer a solution without them. One day I will work on without them, yes. LibreOffice shelters me since long and the operating system will follow.

Here is a list of servers I would prefer to contact, but I do not know how. http://4.2.2.1 does not work.
https://sebastianhetzel.net/zuverlaessige-oeffentliche-dns-server/

Bogen85

  • Hero Member
  • *****
  • Posts: 572
Re: is there an internet connection?
« Reply #44 on: February 15, 2023, 09:52:23 pm »
Here is a list of servers I would prefer to contact, but I do not know how. http://4.2.2.1 does not work.

Because it is for DNS protocol, not HTT Protocol.

https://sebastianhetzel.net/zuverlaessige-oeffentliche-dns-server/

For each of the addresses you need to know the protocol needed to connect to them.
(Or what protocols they understand and respond to)

Looking at that page, all appear to be DNS, so you would need to issue DNS queries using them

But... you might ask, why would DNS be suggested, when DNS is not wanted to be relied on.

Because DNS queries do not require your DNS setup to be working...

4.2.2.1 does respond to ping though (the ICM Protocol).
« Last Edit: February 15, 2023, 09:53:59 pm by Bogen85 »

 

TinyPortal © 2005-2018