Which essentially what MSN messenger does at startup time.
It tries to connect on port 80 to a range of microsoft web servers to determine if it's got connectivity.
I traced the network packets using Ethereal.
Printable View
Which essentially what MSN messenger does at startup time.
It tries to connect on port 80 to a range of microsoft web servers to determine if it's got connectivity.
I traced the network packets using Ethereal.
Quote:
Originally Posted by Bob Sheep
You might however want to skip the connect part, especially if you only have one update server, and you have to check the IP availability not only at startup, but for example every 5 minutes. If all users would try to connect to the same serve, it could bring down the server.
gethostbyaddr does not connect to it, this is why I prefer only to resolve it's address.
i managed to fins a C# Solution for this problem. the way is to set a listener on
the Network Adapter.
However, as i am a Java programmer, i have to do it in c++.
If anyone can convert this code to work as a c++ dll, please replay.
Ohad.
here is the code :
using System;
using System.Management;
// This example demonstrates how to subscribe to an event using the ManagementEventWatcher object.
class Class1
{
public static void Main(string[] args) {
//For the example, we'll put a class into the repository, and watch
//for class deletion events when the class is deleted.
ManagementClass newClass = new ManagementClass();
//Set up an event watcher and a handler for the event
ManagementEventWatcher networkAdapterArrivalWatcher =
new ManagementEventWatcher("\\root\\wmi",
"SELECT * FROM MSNdis_NotifyAdapterArrival ");
ManagementEventWatcher networkAdapterRemovalWatcher =
new ManagementEventWatcher("\\root\\wmi",
"SELECT * FROM MSNdis_NotifyAdapterRemoval " );
MyHandler handler = new MyHandler();
networkAdapterArrivalWatcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);
networkAdapterRemovalWatcher.EventArrived += new EventArrivedEventHandler(handler.Removed);
//Start watching for events
networkAdapterArrivalWatcher.Start();
networkAdapterRemovalWatcher.Start();
while(true)
System.Threading.Thread.Sleep(200000);
//return 0;
}
public class MyHandler {
public void Arrived(object sender, EventArrivedEventArgs e) {
Console.WriteLine("Network connected");
}
public void Removed(object sender, EventArrivedEventArgs e) {
Console.WriteLine("Network disconnected");
}
}
}
The online function always return true though i am working in offline. Please tell me what is problem. I aam connected to Internet by LAN.
Quote:
Originally Posted by kochhar
Hi,
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.
cheers,
Dennis
testIC.bat just reads:Code:#include <stdio.h>
#include <string.h>
#include <process.h>
int main(int argc, char* argv[])
{
FILE *f1;
int check;
char fbuf[150];
// try to ping 66.102.11.104 (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",
"66.102.11.104", "c:\\windows\\testIC.tmp", NULL );
if( check == -1 )
// error from spawnl
return -1;
f1 = fopen( "c:\\windows\\testIC.tmp", "rb" );
if( !f1 )
// error opening file
return -1;
// read the first 149 bytes from testIC.tmp
fread( fbuf, 149, 1, f1 );
fbuf[149] = NULL;
fclose( f1 );
if( strstr( fbuf, "Reply from" ) != NULL )
// reply received, internet connection viable
return 1;
// no reply received, assume internet connection lost
return 0;
}
Code:ping -w %1 -n %2 %3 > %4
_
You should try the DNS name resolution instead of ping (gethostbyname). That will probably not be disabled by such routers.