Recent

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

Nicole

  • Hero Member
  • *****
  • Posts: 970
is there an internet connection?
« on: February 03, 2023, 01:33:44 pm »
I took a method out of some source and have no idea, how it works:

Code: Pascal  [Select][+][-]
  1. function InternetConnected: Boolean;
  2.  
  3. var
  4.   dwConnectionTypes: DWord;
  5. begin
  6.   dwConnectionTypes := INTERNET_CONNECTION_MODEM or INTERNET_CONNECTION_LAN or INTERNET_CONNECTION_PROXY;
  7.   Result := InternetGetConnectedState(@dwConnectionTypes, 0);
  8. end;

What I want to: Check, if the internet connection exists.
The check produces false positives.

Any ideas, why this may be?

At the moment it starts from within a VMWare-VM under Win 7 and has a NAT connection.

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: is there an internet connection?
« Reply #1 on: February 03, 2023, 01:38:58 pm »
Always try to read the original documentation first. It helps in understanding what the function is suppose to do.

https://learn.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetgetconnectedstate

There it states:
Quote
Note  Using this API is not recommended, use the INetworkListManager::GetConnectivity method instead.


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: is there an internet connection?
« Reply #2 on: February 03, 2023, 01:42:33 pm »
See this  answer, and the others in the same thread

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: is there an internet connection?
« Reply #3 on: February 03, 2023, 01:44:47 pm »
Thank you for the link.
The problem is: I do not understand the details. Neither in "my" source, nor in the links.

Do you think, you can just copy me the correct line form there?
The only thing I need, is a result only true, if there is a connection.

I do not want to understand it, my work is something completely different.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: is there an internet connection?
« Reply #4 on: February 03, 2023, 03:27:21 pm »
The problem is: I do not understand the details.
What you not understand, do it like link provided and you have a proper response.
I do not want to understand it
That is the worst a programmer can say, Pascal ain't a script language where you just copy/paste.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: is there an internet connection?
« Reply #5 on: February 03, 2023, 04:06:01 pm »
What an answer!
If you don’t want to understand then why program at all?
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: is there an internet connection?
« Reply #6 on: February 03, 2023, 04:16:17 pm »
Pascal ain't a script language where you just copy/paste.

 :D Haha!

Even in scripting languages blind copypasta without understanding what the copypasta is and does is a recipe for disaster! :D

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: is there an internet connection?
« Reply #7 on: February 03, 2023, 05:16:13 pm »
The only thing I need, is a result only true, if there is a connection.

"Is there a connection" is a very broad and nebulous term.

My computer could have access to thousands of computer and websites, and not one of those be on the Internet.

A connection to what? Yes, I get it, the Internet...

But it is not simple check. Yes, high level tools do give you a simple status, because they are checking the ability to access to access some specific resource (or at least one of multiple resources) that can only be found on the Internet.

Hence what is discussed in the given topic/thread: https://forum.lazarus.freepascal.org/index.php/topic,43819.0.html


Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: is there an internet connection?
« Reply #8 on: February 03, 2023, 05:21:34 pm »
Do you think, you can just copy me the correct line form there?
The only thing I need, is a result only true, if there is a connection.

You might think it is simple, it is not.
Hence you need to understand it.

Checking for "an Internet connection" is complex. So you need to have some understanding of what is being checked for.

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 396
Re: is there an internet connection?
« Reply #9 on: February 03, 2023, 06:48:24 pm »
I think you just want to see if you can contact other computers that are connected to the internet.  If that's correct and you're using Windows, the easiest way to do this is to "ping" another computer that you know is in fact connected.  A way to do that is illustrated in this thread:

https://forum.lazarus.freepascal.org/index.php?topic=39409.0

(Hope it helps.  You can make a career out of learning the ins and outs of the internet, so cut-and-paste is OK, IMHO, if you don't want to  :) )


af0815

  • Hero Member
  • *****
  • Posts: 1289
Re: is there an internet connection?
« Reply #10 on: February 03, 2023, 07:08:48 pm »
If you have WIndows and block in a external firewall one server, your PC will always say "No Internet".

So you can only say, you have a connection to a server in internet and this mean you have internet. You can start a request to google or your own homepage or a ping to a known server (as Curt say) or read a ntp for time,...

So its your choice to say: I have internet, because xxx work.
regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: is there an internet connection?
« Reply #11 on: February 03, 2023, 07:17:01 pm »
"ping"
Pings are dropped by any sane servers.... Bad advice.
Specialize a type, not a var.

af0815

  • Hero Member
  • *****
  • Posts: 1289
Re: is there an internet connection?
« Reply #12 on: February 03, 2023, 07:18:26 pm »
BTW: M$ marked the InternetGetConnectedState as "Using this API is not recommended"
regards
Andreas

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: is there an internet connection?
« Reply #13 on: February 03, 2023, 08:21:38 pm »
So you can only say, you have a connection to a server in internet and this mean you have internet. You can start a request to google or your own homepage or a ping to a known server (as Curt say) or read a ntp for time,...

So its your choice to say: I have internet, because xxx work.

Agreed.

However....
  • The particular server may go down or lose its connection
  • If google, it has gone down before, but that is rare...
  • The server your website is on might go down or lose its connection
  • The server you were pinging might go down, lose its connection, or be reconfigured to stop accepting pings
  • NTP, same as point #1

What really matters is the Internet resources your app needs to connect to.

However...
  • Are you accessing by address? What happens if the address changes?
  • Is the DNS round robin? Are you caching name lookups? What happens if you get a stale one?
  • What happens if your access to DNS is interrupted or the DNS you use goes down?

So, what does it mean to have an "Internet connection"?

If I can't access DNS correctly but I can connect to sites by address, does that mean I have an "Internet connection"?
If I can use DNS and can access some sites, but not the one I'm checking on or need, does that mean I do not have an "Internet connection"?

True, OP Nicole does not need to know all that is involved or have a deep understanding of DNS, routers, firewalls, gateways, NAT, VPN, proxies, etc...

But OP Nicole needs to establish what an "Internet connection" means for said application, and go from there...
« Last Edit: February 03, 2023, 08:24:17 pm by Bogen85 »

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: is there an internet connection?
« Reply #14 on: February 04, 2023, 10:50:47 am »
ok, then I put it differently:

When anything goes wrong with the internet, I do something what I do not understand neither, but does the job in absolute every of my needs since many years without exception.

(windows)
Win-key + r
cmd
ping 4.2.2.1
(no idea who is this server and I do not care)

The man that days says, this "checks for the internet".
And it does.
Either I see the ping result in milliseconds or a "timeout".

This job I want my software doing - instead of my fingers in the command-shell.
Need not be the command shell, can be every kind of line or ping.
Best of all it would be my ping above.

Unfortunately I do not know, how to send a ping by Lazarus and how to grab its results.


 

TinyPortal © 2005-2018