tags:

views:

184

answers:

1

Hello,

VS 2008

I am developing an application that has to detect whether a client has a network connection. i.e. LAN cable plugged in or wireless switched on. I have been using the code below.

I am using the NetworkAvailabilitychangedEvent to fire an event when their wireless is turned off or the cable has been pulled out. However, this only works if the user has only 3 connections present (LAN, Wireless, and loopbacks).

Microsoft: "The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface".

However, some client has more than 3 connections. One client had a bluetooth connection and someone else had some VMWare connections. On these client it failed to fire the event.

Is there anyway I can ignore all these connections I am not interested in listening out for, and just listen on the LAN and Wireless?

Many thanks for any advice,

private void Form1_Load(object sender, EventArgs e)
    {
        NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(OnNetworkChangedEvent);  
    }

    private void OnNetworkChangedEvent(object sender, NetworkAvailabilityEventArgs e)
    {
        bool available = e.IsAvailable;

        NetworkInterface[] networkConnections = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface ni in networkConnections)
        {
            if (ni.Name == "Local Area Connection")
            {
                if (ni.OperationalStatus == OperationalStatus.Down)
                {
                    Console.WriteLine("LAN disconnected: " + ni.Description);
                }
            }
            else if (ni.Name == "Wireless Network Connection")
            {
                if (ni.OperationalStatus == OperationalStatus.Down)
                {
                    Console.WriteLine("Wireless disconnected: " + ni.Description);
                }
            }
        }
    }
A: 

Perhaps you need to implement some sort of polling agent that runs on a seperate thread and regularly tries to contact something listening on a remote server (or servers?).

When it fails (or perhaps when a preset number of connection attempts fail) then fire an event. Same when it goes from a fail-succeed state.

Your application would subscribe to these events and take action accordingly.

Of course this may not be suitable for your scenario..

tomfanning
Hello, I am interested in what you have said. Do you know of any design pattern that implement this? Any articles? Thanks
robUK
No, I just came up with it.It really wouldn't be that hard to implement from scratch...
tomfanning