Page 1 of 1
detect if connected to internet
Posted: Tue Mar 25, 2008 5:02 pm
by quasiperfect
how can i detect if a computer is connected to internet ?
non lan connection but internet connection becase i use adsl with ppoe
i use ReceiveHTTPFile to get a file from internet when the connection is active it works great but when is down it crashes my app.
Posted: Tue Mar 25, 2008 6:19 pm
by DoubleDutch
Do a search on the forum for InternetGetConnectedState_
I think you should use:
Code: Select all
If InternetGetConnectedState_(#Null,#Null)
... Internet code here ...
EndIf
Posted: Tue Mar 25, 2008 7:18 pm
by quasiperfect
thanks for responding dosen't work with that function i tried with the solution form here
http://www.purebasic.fr/english/viewtop ... ectedstate
Code: Select all
; English forum:
; Author: MrVainSCL
; Date: 19. January 2003
;Here is a normal version:
; ------------------------------------------------------------
;
; PureBasic Win32 API - CheckInternetConnection - Example v1.0
;
; by MrVainSCL! aka Thorsten 19/Jan/2003 PB v3.51+
;
; ------------------------------------------------------------
;
If InternetGetConnectedState_(0, 0)
result$ = "Online"
Else
result$ = "Offline"
EndIf
;
MessageRequester("Are we connected to the Internet?","We are "+result$,0)
End
;
; ------------------------------------------------------------
;Here is a Procedure() version:
; ------------------------------------------------------------
;
; PureBasic Win32 API - CheckInternetConnection - Example v1.0
;
; by MrVainSCL! aka Thorsten 19/Jan/2003 PB v3.51+
;
; ------------------------------------------------------------
;
Procedure MyCheckInternetConnection()
If InternetGetConnectedState_(0, 0)
result = 1
Else
result = 0
EndIf
;
ProcedureReturn result
EndProcedure
;
; -------- Check if user is connected to internet --------
;
If MyCheckInternetConnection() = 1
state$ = "Online"
Else
state$ = "Offline"
EndIf
;
MessageRequester("Are we connected to the Internet?","We are "+state$,0)
End
and it tells me i'm conected and i'm not
any other idea ?
Posted: Tue Mar 25, 2008 9:11 pm
by TerryHough
It has been awhile since I tested this, but seems to be working here.
Give it a try to see if fits your needs.
Code: Select all
; HowIsInternet - 09/22/2003 updated by TerryHough
; (c) ses007, 2003
; From PB forum, topic
; http://purebasic.myforums.net/viewtopic.php?t=5590
#INTERNET_CONNECTION_MODEM = $1
#INTERNET_CONNECTION_LAN = $2
#INTERNET_CONNECTION_PROXY = $4
#INTERNET_CONNECTION_MODEM_BUSY = $8
#INTERNET_CONNECTION_OFFLINE = $20
#INTERNET_CONNECTION_CONFIGURED = $40
#INTERNET_RAS_INSTALLED = $10
;Global dwflags.b
Global sFlags.s
Global msg.s
Procedure.s InternetStatus()
Protected dwflags.b
If InternetGetConnectedState_(@dwflags, 0)
sFlags.s = RSet(Bin(dwflags),8,"0")
If Mid(sFlags,8,1) = "1" ; = #INTERNET_CONNECTION_MODEM
msg + "The Internet connection is made by a modem" + Chr(10)
EndIf
If Mid(sFlags,7,1) = "1" ; = #INTERNET_CONNECTION_LAN
msg + "The Internet connection is made via a network (LAN)"
EndIf
If Mid(sFlags,6,1) = "1" And Mid(sFlags,4,1) = "0" ; = #INTERNET_CONNECTION_PROXY
msg + " and using a Proxy server." + Chr(10)
Else
msg + "." + Chr(10)
EndIf
If Mid(sFlags,6,1) = "1" And Mid(sFlags,4,1) = "1" ; = #INTERNET_CONNECTION_OFFLINE
msg + "The Internet connection is currently offline. " + Chr(10)
EndIf
If Mid(sFlags,5,1) = "1" And Mid(sFlags,7,1) = "1" ; = #INTERNET_RAS_INSTALLED
msg + "A Remote Access Service (RAS) is installed." + Chr(10)
EndIf
If Mid(sFlags,5,1) = "1" And Mid(sFlags,6,1) = "0" ; = #INTERNET_CONNECTION_MODEM_BUSY
msg + "The modem is busy with another connection." + Chr(10)
EndIf
If Mid(sFlags,3,1) = "1" And Mid(sFlags,5,1) = "1" ; = #INTERNET_CONNECTION_CONFIGURED
msg + "An Internet connection is configured." + Chr(10)
EndIf
If Mid(sFlags,4,1) = "1" And Mid(sFlags,6,1) = "1" ; = #INTERNET_CONNECTION_OFFLINE
msg + "The Internet connection is currently offline. " + Chr(10)
EndIf
;msg + "dwFlags: " + sFlags + Chr(10) + Chr(10)
Else
msg = "Currently, there is no Internet connection." + Chr(10) + Chr(10)
EndIf
ProcedureReturn msg
EndProcedure
msg = InternetStatus()
Result.l = InternetAttemptConnect_(0)
If Result = #ERROR_SUCCESS
msg + "This system can connect to the Internet."
Else
msg + "This system cannot connect to the Internet."
errNumber.l = GetErrorNumber()
errDescription.s = GetErrorDescription()
msg + "Error Number: " + Str(errNumber) + Chr(10) + errDescription
EndIf
MessageRequester("Internet Connection", msg, #MB_ICONINFORMATION)
Posted: Wed Mar 26, 2008 8:23 am
by quasiperfect
nope it dosen't work
if the pppor connection is on it tells me that i can connect and i use a modem but it dosen't tell me if i'm connected or not
if the pppoe is disconected and lan active it tells me i'm using lan and that the system can connect
Posted: Wed Mar 26, 2008 9:14 am
by DoubleDutch
Do you have some kind of caching proxy server running like 602LAN?
Posted: Wed Mar 26, 2008 9:39 am
by Hydrate
One simple solution might be to ping google.co.uk, that wont crash your app if there is no reply and googles servers are almost always up (99.9% of hte time). If there is no reply you know you are not connected, if there is, you are.
I know it doesnt do exactly what you are asking, but you could use it with the internet status thing, if its connected and connects to google you are connected, if not, you arnt. Just a thought.
Posted: Wed Mar 26, 2008 10:08 am
by quasiperfect
nope no proxy or anything like that
just a lan card connected to a adsl modem.
i preaty much solved it like this i'm not sure if is the proper way to do things
all code is taken from the forums all credits go to the respective developers, thanks guys
Code: Select all
Structure EchoResult
Reply.ICMP_ECHO_REPLY
Buffer.l[20]
EndStructure
InitNetwork()
Procedure.s GetIPbyName (NameIP.s)
TheIPAdress.s
pHostinfo = gethostbyname_(NameIP)
If pHostinfo = 0
TheIPAdress = "0"
Else
CopyMemory (pHostinfo, hostinfo.HOSTENT, SizeOf(HOSTENT))
If hostinfo\h_addrtype <> #AF_INET
TheIPAdress = "0"
Else
While PeekL(hostinfo\h_addr_list+AdressNumber*4)
ipAddress = PeekL(hostinfo\h_addr_list+AdressNumber*4)
TheIPAdress = StrU(PeekB(ipAddress),0)+"."+StrU(PeekB(ipAddress+1),0)+"."+StrU(PeekB(ipAddress+2),0)+"."+StrU(PeekB(ipAddress+3),0)
AdressNumber+1
Wend
EndIf
EndIf
ProcedureReturn TheIPAdress
EndProcedure
Procedure Ping(Address$, Timeout)
If GetIPbyName (Address$) <> "0"
Address$ = GetIPbyName (Address$)
hPort = IcmpCreateFile_()
Result = IcmpSendEcho_(hPort, inet_addr_(@Address$), @"Echo This Information Back To Me", Len("Echo This Information Back To Me"), 0, @ECHO.EchoResult, SizeOf(EchoResult), Timeout)
Risposta=ECHO\Reply\Status
IcmpCloseHandle_(hPort)
If Result = 0
ProcedureReturn -1
Else
If Risposta=0 ; SUCCES
ProcedureReturn ECHO\Reply\RoundTripTime
Else
ProcedureReturn -1
EndIf
EndIf
Else
ProcedureReturn -1
EndIf
EndProcedure
Procedure update()
a$=Space(999) : GetModuleFileName_(0,@a$,999) : appdir$=GetPathPart(a$)
If InternetGetConnectedState_(0, 0)
If Ping("www.anysite.com",370) <> -1
If ReceiveHTTPFile("http://www.anysite.com/file.ext", appdir$+"file.ext") = 1
MessageRequester("Message", "file saved",#PB_MessageRequester_Ok)
Else
MessageRequester("Errore", "file save failed",#PB_MessageRequester_Ok)
EndIf
Else
MessageRequester("Errore", "no response from site",#PB_MessageRequester_Ok)
EndIf
Else
MessageRequester("Errore", "no connection",#PB_MessageRequester_Ok)
EndIf
EndProcedure