ran into this problem of having to detect the presence of an internet connection, found this thread and decided to post my VERY unsatisfying, but working, 'solution'. Especially the part where it needs a batch file is extremely annoying.
Moreover, as discussed in this thread, this 'solution' obviously doesn't work from behind routers that disabled pinging.
int main(int argc, char* argv)
// try to ping 18.104.22.168 (www.google.nl), just ONCE, waiting for MAX 500 msec
// batch file needed because ping doesn't take "> testIC.bat" for an argument
check = spawnl( P_WAIT, "c:\\windows\\testIC.bat", "testIC.bat", "500", "1",
"22.214.171.124", "c:\\windows\\testIC.tmp", NULL );
if( check == -1 )
// error from spawnl
f1 = fopen( "c:\\windows\\testIC.tmp", "rb" );
if( !f1 )
// error opening file
// read the first 149 bytes from testIC.tmp
fread( fbuf, 149, 1, f1 );
fbuf = NULL;
fclose( f1 );
if( strstr( fbuf, "Reply from" ) != NULL )
// reply received, internet connection viable
// no reply received, assume internet connection lost
What platform are you testing this on? I tried my function
both at work (Win/XP, VS v6, ethernet (twiested pair))
and at home (Win/XP, VS v6, modem) and every time it
returned the correct answer within a second or less.
My ethernet card (actually it's built into the motherboard)
is an "Intel(R) PRO/100 VE". Maybe that makes a difference..
Originally posted by jase jennings
I've no doubt your function works fine with a dial up modem, but not when you are using a router / lan
I tested as follows:
(1) Ethernet cable connected: returned TRUE
(2) Disconnect cable: returnd FALSE
(3) Reconnected cable: returned TRUE
I suppose there must be cases where it fails, but I don't
know what they are or why.
I am attaching a small program that shows how to ping
from your program. Most of the code was posted by
Steve Bryndin at Codeguru. I just modified in small ways.
Note that it does a lot more than just ping a number, e.g.
you can ask it to ping by name, etc. But it works well.