Check if user is connected to the internet

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Check if user is connected to the internet

Post by Quin »

I have an old app that uses the result of InitNetwork() to determine if the user is connected to the internet or not. I'm not 100% certain if this was correct in retrospect, but it worked. However, InitNetwork() is now removed. Is this still possible to do somehow?
Thanks.
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Check if user is connected to the internet

Post by jacdelad »

Hi Quin!
viewtopic.php?t=31623

Short answer: Windows offers the API function InternetGetConnectedState_(), which can be used like in the thread linked. Someone states it's not reliable in some cases, so i would simply try to connect to google.com or something similar (with ReceiveHTTPFile, which would make it multiplatform available).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
kenmo
Addict
Addict
Posts: 2051
Joined: Tue Dec 23, 2003 3:54 am

Re: Check if user is connected to the internet

Post by kenmo »

Something like this? :)

Code: Select all

Procedure.i IsConnectedToInternet()
  Connected = #False
  CompilerIf #PB_Compiler_Version < 600
    If (InitNetwork())
      *Buffer = ReceiveHTTPMemory("captive.apple.com", #PB_HTTP_NoRedirect)
    EndIf
  CompilerElse
    *Buffer = ReceiveHTTPMemory("captive.apple.com", #PB_HTTP_NoRedirect)
  CompilerEndIf
  If (*Buffer)
    Connected = Bool(FindString(PeekS(*Buffer, MemorySize(*Buffer), #PB_Ascii), "Success"))
    FreeMemory(*Buffer)
  EndIf
  ProcedureReturn Connected
EndProcedure

Debug IsConnectedToInternet()
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Check if user is connected to the internet

Post by RASHAD »

Using Windows API

Code: Select all

#FLAG_ICC_FORCE_CONNECTION = 1

If InternetCheckConnection_(@"http://www.google.com",#FLAG_ICC_FORCE_CONNECTION,0) = 1
   Debug "OK"
Else
   Debug "NO"
EndIf
Egypt my love
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Check if user is connected to the internet

Post by PBJim »

I saw a comment elsewhere that suggests the InternetCheckConnection API is deprecated and does not work with proxy servers.

INetworkListManager::GetConnectivity method (netlistmgr.h) was recommended :

https://learn.microsoft.com/en-us/windo ... nnectivity
User avatar
blueb
Addict
Addict
Posts: 1118
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Check if user is connected to the internet

Post by blueb »

Saw this from mk-soft

Code: Select all

;- mk-soft May 17, 2021

; INTERNET_CONNECTION_CONFIGURED; 0x40
; Local system has a valid connection to the Internet, but it might or might not be currently connected.
; INTERNET_CONNECTION_LAN; 0x02
; Local system uses a local area network to connect to the Internet.

; INTERNET_CONNECTION_MODEM; 0x01
; Local system uses a modem to connect to the Internet.

; INTERNET_CONNECTION_MODEM_BUSY; 0x08
; No longer used.

; INTERNET_CONNECTION_OFFLINE; 0x20
; Local system is in offline mode.

; INTERNET_CONNECTION_PROXY; 0x04
; Local system uses a proxy server to connect to the Internet.

; INTERNET_RAS_INSTALLED; 0x10
; Local system has RAS installed.

lpdwFlags.l
r1 = InternetGetConnectedState_(@lpdwFlags, 0)
If r1
  Debug "Online"
Else
  Debug "Offline"
EndIf

Debug "Flags: 0x" + Hex(lpdwFlags)

- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Check if user is connected to the internet

Post by Quin »

Thanks all for your answers!
Ended up going with Kenmo's solution as I need this app to be cross-platform, but I can at least say I learned something about the Windows API today :)
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Check if user is connected to the internet

Post by BarryG »

Does Kenmo's solution work on a non-English OS? It checks for the word "Success" which might not be the return value on a non-English OS, or on the remote server.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Check if user is connected to the internet

Post by infratec »

Then you should use timeout.
Else you have to wait a long time if you have no connection.

Code: Select all

Procedure.i IsConnectedToInternet()
  
  Protected.i Connected, HTTPRequest
  
  HTTPTimeout(1000)
  HTTPRequest = HTTPRequest(#PB_HTTP_Get, "https://captive.apple.com", "", #PB_HTTP_HeadersOnly|#PB_HTTP_NoSSLCheck)
  If HTTPRequest
    Connected = #True
    FinishHTTP(HTTPRequest)
  EndIf
  ProcedureReturn Connected
EndProcedure

Debug IsConnectedToInternet()
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Check if user is connected to the internet

Post by infratec »

But I think this is faster and takes less resources:

Code: Select all

Procedure.i IsConnectedToInternet()
  
  Protected.i Connection
  
  Connection = OpenNetworkConnection("captive.apple.com", 443, #PB_Network_TCP, 500)
  If Connection
    CloseNetworkConnection(Connection)
  EndIf
  
  ProcedureReturn Connection
  
EndProcedure

If IsConnectedToInternet()
  Debug "Connected"
Else
  Debug "Not connected"
EndIf
Because this opens only the connection without making a ssl handshake.
You can check this stuff with wireshark to see the differences.
Little John
Addict
Addict
Posts: 4803
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Check if user is connected to the internet

Post by Little John »

Thanks infratec, much appreciated!
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Check if user is connected to the internet

Post by Quin »

BarryG wrote: Thu Oct 03, 2024 10:19 am Does Kenmo's solution work on a non-English OS? It checks for the word "Success" which might not be the return value on a non-English OS, or on the remote server.
Don't think Apple's captive server checks the locale of the user pinging it, so it should
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Check if user is connected to the internet

Post by Quin »

infratec wrote: Thu Oct 03, 2024 11:28 am But I think this is faster and takes less resources:

Code: Select all

Procedure.i IsConnectedToInternet()
  
  Protected.i Connection
  
  Connection = OpenNetworkConnection("captive.apple.com", 443, #PB_Network_TCP, 500)
  If Connection
    CloseNetworkConnection(Connection)
  EndIf
  
  ProcedureReturn Connection
  
EndProcedure

If IsConnectedToInternet()
  Debug "Connected"
Else
  Debug "Not connected"
EndIf
Because this opens only the connection without making a ssl handshake.
You can check this stuff with wireshark to see the differences.
A wizard as always, Infratec. Thanks a ton! :)
Post Reply