View Full Version : [C++] Check internet connection
ggambett
12-19-2003, 05:14 AM
The first time BBB is run, it asks the user for his email, and tries to do a HTTP POST to my site (please ignore the moral/ethical/bandwidth/global warming implications of this). When the player is connected to the internet, it takes no more than 5 seconds.
The problem is when the user is not connected. It takes an annoyingly long time. The function that takes time is HttpSendRequest() (I'm using wininet).
I tried lowering the timeout values and the retries with InternetSetOption(), and although the functions succeeded, there was no visible effect.
I also tried using InternetCheckConnection(), InternetQueryOption(INTERNET_OPTION_CONNECTED_STATE) but the returned values made no sense.
What do you suggest? I'm thinking about trying to open a socket and try to resolve a site name or trying to connect to it, but is it necessary to go that low level?
Jake Stine
12-19-2003, 06:10 AM
It looks to me like your best solution is to implement an asynchronous call. From the looks of it, setting up asynchronous internet operations is pretty easy and won't require you to alter any of your game execution code. You just set up an independent callback which handles messages that come in from the WinINet functions.
MSDN Link on Asynch WinINet Stuff (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/calling_wininet_functions_asynchronously.asp)
I read around a bit and it looks like this is the accepted way to avoid the dreaded wait of doom on machines not connected to the net or behind a firewall. And take it from a winsock user, this looks a heck of a lot easier than trying to set up a basic low-level winsock poll (which still needs to be asynchronous anyways to avoid the same delays). :)
- Air
BitBoy
12-19-2003, 06:20 AM
On a side note, how do programs get notified when the user connects to the internet? ICQ does this, as well as many other programs (I guess). Not that I really need to know, it just popped into my mind when I read this thread...
Sorry for interfering. I'll be quiet now!
Jake Stine
12-19-2003, 10:24 AM
That's a good question. I know it has something to do with Dial-up Networking features-- that's why programs like ICQ will ask you if you are on a dial-up or LAN when you install or first run it. If you select LAN, ICQ won't wait for the DUN notifications and tries to talk to the internet immediately. Likewise if you select DUN but are on a cablemodem or LAN, ICQ won't bother trying to connect to the net until you manually open the contact list and force it to go online.
But I couldn't find anything pertinent regarding DUN notifications in doing some searches online and in the MSDN, although granted I didn't look very hard. :)
- Air
sordith
12-19-2003, 11:31 AM
A second option to making it an async call is to put your call to HttpSendRequest() in a seperate thread.
// Where you are currently making your HttpSendRequest() call:
_beginthread(threadFunc, 0, &EmailAddress);
void threadFunc(void *ptr)
{
// Make a quick copy of ptr which should be your email address.
// Make HttpSendRequest() call
}
To avoid any threading ugliness, EmailAddress should be a copy of the email address only used once for the thread.
princec
12-20-2003, 01:54 AM
The way I did it (in Java but should work just the same in C) was to get the local internet addresses for all the adapters in the machine and see if any of them weren't 127.0.0.1 - which generally means the machine is on the internet, or that it's on a LAN and probably on the internet.
Cas :)
patrox
12-20-2003, 02:36 AM
Originally posted by ggambett
The first time BBB is run, it asks the user for his email, and tries to do a HTTP POST to my site (please ignore the moral/ethical/bandwidth/global warming implications of this). When the player is connected to the internet, it takes no more than 5 seconds.
How about not connecting, but asking on exit of the game and then do a
ShellExecute( "open" , "http://www.bbb.com?subscribe=email@email.com" ) ?
pat.
ggambett
12-22-2003, 06:13 AM
princec : Hmmm. In my home LAN I have 192.168.0.x and there's no internet connection...
patrox : The problem with that is that I don't have any means of knowing the result of opening the page, let alone trying to read something back. In this case I could do ShellExecute, that's what I'm doing with the Buy Now button, but what if I wanted to send an authorization code and read the result back from the site? I can't do that.
Sordith, Jake : Same thing... I want this thing to be blocking, but not for that long.
princec
12-22-2003, 06:16 AM
It's still a reasonably useful way of checking for internet connection. It definitely detects the case where there is definitely no internet connection.
Cas :)
Jake Stine
12-22-2003, 07:52 AM
Originally posted by ggambett
Sordith, Jake : Same thing... I want this thing to be blocking, but not for that long.
I looked around a little more on the MSDN and found this function:
InternetGetConnectedState (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetgetconnectedstate.asp) -- This should do almost exactly what you want. However, there are some conflicting information on its proper use. You'll want to read this article (http://www.microsoft.com/mind/1297/inet/inet.asp) and decide if the examples in it are more valid than the MSDN docs themselves. :)
Alternatively there are these:
InternetCheckConnection (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetcheckconnection.asp) -- however there's no information on how long a call to this function takes, but from what it says I'm pretty sure its nearly instantaneous. There is another function InternetAttemptConnect (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetattemptconnect.asp), which is indeed nearly instantaneous. However, it will bring up a DUN connect box on people who have DUN but are not currently online, which might not be as covert as you might want.
Those are your only options other than the asynchronous approach. (this is speculation on my part ->) I think problem is that Microsoft has hard coded limits on connection timeouts in the registry. Timeouts can't seem to go lower than those values. I can only assume that it's Microsoft's way of putting in safeguards against coders setting values that simply aren't safe/reliable for standard internet trafficking (because really they're at bare minimums already for most sub-28.8 connections, all 15 of them that might still exist heh).
- Air
gilzu
12-22-2003, 09:34 AM
theres one other thing you should consider:
here in israel, those who have cable connection can access a website that shows varius ISP for you to choose. If you are not logged, every HTTP request will be redirected to that site which is a real mess when dealing with programs like yours or icqdetect, nav ect.
so actually, i'm ALWAYS shown as if i'm connected