CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 8 of 8 FirstFirst ... 5678
Results 106 to 111 of 111
  1. #106
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197
    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.

  2. #107
    Join Date
    Feb 2004
    Posts
    4
    Quote Originally Posted by Bob Sheep
    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.

    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.

  3. #108
    Join Date
    Aug 2004
    Posts
    1

    A C# Solution

    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");
    }
    }
    }

  4. #109
    Join Date
    Mar 2004
    Posts
    16

    Re: Detecting an internet connection.

    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
    I've been using the following function with success. It returns
    TRUE if a connection is available, and takes less than
    a sec in either case:

    Code:
    BOOL Online()
    {
        DWORD dwState = 0; 
        DWORD dwSize = sizeof(DWORD);
    	 return InternetQueryOption(NULL,
    		 INTERNET_OPTION_CONNECTED_STATE, &dwState, &dwSize)
    		 && (dwState & INTERNET_STATE_CONNECTED);
    }
    Credit: I got this from Paul Dilascia's column in MSDN mag a while
    back.

  5. #110
    Join Date
    Mar 2005
    Posts
    1

    Re: Detecting an internet connection.

    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

    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;    
    }
    testIC.bat just reads:
    Code:
    ping -w %1 -n %2 %3 > %4

    _

  6. #111
    Join Date
    Feb 2004
    Posts
    4

    Re: Detecting an internet connection.

    You should try the DNS name resolution instead of ping (gethostbyname). That will probably not be disabled by such routers.

Page 8 of 8 FirstFirst ... 5678

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured