Jase,
I cant thank you enough for your very lucid explanation of a method of 'Check for Updates'. I will give it a try.
Thanks again.
Mike
Printable View
Jase,
I cant thank you enough for your very lucid explanation of a method of 'Check for Updates'. I will give it a try.
Thanks again.
Mike
No problem. I hope my explanation was helpful. Ask more questions if I was unclear :)Quote:
Originally posted by Mike Pliam
Jase,
I cant thank you enough for your very lucid explanation of a method of 'Check for Updates'. I will give it a try.
Thanks again.
Mike
Hi!
If you don't want to ping, you only want to check for a connection from a computer to another computer (LAN or WAN), you can use the following code (unfortunately no support for Win95; but only for Win98 or higher, Win NT4 or higher).
int main(int argc, char* argv[])
{
PMIB_IFTABLE pMIBTable;
char* pBuffer;
DWORD uResult;
DWORD uSize = 32768;
int i;
bool bConnected = false;
pBuffer = new char[uSize];
pMIBTable = (PMIB_IFTABLE)pBuffer;
uResult = GetIfTable ( pMIBTable, &uSize, false );
if ( uResult == NO_ERROR )
{
for ( i = 0; i < pMIBTable->dwNumEntries; i ++ )
{
if ( pMIBTable->table[i].dwType != MIB_IF_TYPE_LOOPBACK )
{
if ( pMIBTable->table[i].dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED
|| pMIBTable->table[i].dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL )
{
bConnected = true;
}
}
}
}
delete[] pBuffer;
return 0;
}
Sorry, but if you have two computers, one connecting to the internet through the another, and you check the state on the computer which is not directly connected to the internet, the result MUST be connected if the computers are connected, and it is irrelevant the state of the internet connection on the other computer.
Don't forget that Internet Explorer has a home page, so it can easily check if the internet is available or not, since it will try to access the home page.
Hope the above code helps.
Cheers!
I found out later, that unfortunately the above code does not work as it should for NT/2K platforms, even if the function exists.
But I managed to solve to problem by using the gethostbyname function, which will attempt to resolve a DNS name of the site that has updates of the software. If it can not resolve the DNS name, than there is no IP connection.
And best of all, if you have a modem connection, it will not trigger the dialup screen.
try this
int nInit = AfxSocketInit();
CSocket sock;
if(sock.Create()&&nInit)
{
if(sock.Connect("www.google.com",80))
{
AfxMessageBox("connected");
}
sock.Close();
}
although slow but tells the truth..
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.