Internet connection detection
-
well I don't know much about internet programming but I need my application to do some action when the user connects to the internet in other words * while the pragram is running when connected fire some event(or simply call afunction) when connection terminated do othe action thanks for your time
-
well I don't know much about internet programming but I need my application to do some action when the user connects to the internet in other words * while the pragram is running when connected fire some event(or simply call afunction) when connection terminated do othe action thanks for your time
This function determines whether the user is connected to at least one RAS connection. Link with rasapi32.lib. You can use this function to start your program.
#include <windows.h>
#include <ras.h>BOOL IsUserConnected(){
RASCONN *rasc=malloc(1000);
DWORD bs,nc,i,j,dwRet;
RASCONNSTATUS rcs;
MSG msg;
TCHAR s[257];
rcs.dwSize=160;
rasc->dwSize=412;
dwRet=RasEnumConnections(rasc,&bs,&nc);
if(!bs)return 0;
rasc=realloc(rasc,bs);
if(RasEnumConnections(rasc,&bs,&nc))
return 0;
for(i=0;i<bs/412;i++){
HRASCONN hrc=rasc[i].hrasconn;
dwRet = RasGetConnectStatus(hrc, &rcs);
if (dwRet != ERROR_INVALID_HANDLE)
continue;
if (dwRet != ERROR_NOT_ENOUGH_MEMORY){
SetLastError(dwRet);
return 0;
}
if(rcs.rasconnstate==RASCS_Connected)
return 1;
}
return 0;
}Peter O.
-
well I don't know much about internet programming but I need my application to do some action when the user connects to the internet in other words * while the pragram is running when connected fire some event(or simply call afunction) when connection terminated do othe action thanks for your time
Also see
InternetGetConnectedState()
. You can call it in a timer handler and fire events when the connection state changes. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com -
Also see
InternetGetConnectedState()
. You can call it in a timer handler and fire events when the connection state changes. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.comhi ravi this function puzzles me cos it seems to fail when u are connecting thru a shared connection across a local network ... ie, it always thinks ur connected even if the connecting (server) machine isnt actually connected have i got something very wrong?
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
sonork: 100.18128 8028finder.com -
hi ravi this function puzzles me cos it seems to fail when u are connecting thru a shared connection across a local network ... ie, it always thinks ur connected even if the connecting (server) machine isnt actually connected have i got something very wrong?
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
sonork: 100.18128 8028finder.comUgh, thank you Microsoft. :( I haven't tried it in that mode. It seems to work when you're connected thru a LAN or dialed in via a modem. Perhaps Peter's RAS approach is more robust? /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com
-
Ugh, thank you Microsoft. :( I haven't tried it in that mode. It seems to work when you're connected thru a LAN or dialed in via a modem. Perhaps Peter's RAS approach is more robust? /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com
i dont really blame them for bad api functionality cos the only reliable way i found was to try a ping on the server before assuming a connection was actually there ... i found a timeout of 10 seconds gives a fair compromise between a user waiting too long and the net being a little slow if there is a better way i would like to hear it
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
sonork: 100.18128 8028finder.com -
i dont really blame them for bad api functionality cos the only reliable way i found was to try a ping on the server before assuming a connection was actually there ... i found a timeout of 10 seconds gives a fair compromise between a user waiting too long and the net being a little slow if there is a better way i would like to hear it
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
sonork: 100.18128 8028finder.comYou're right. But testing for a ping needs to be done in a separate thread (i.e. in a non-blocking manner) for it to be useful. I can't think of a foolproof and efficient way to do this. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com
-
You're right. But testing for a ping needs to be done in a separate thread (i.e. in a non-blocking manner) for it to be useful. I can't think of a foolproof and efficient way to do this. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com
thats why i use a timeout of 10 seconds ... a klunk of a compromise but at least sanity shows its face occasionally :)
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
sonork: 100.18128 8028finder.com