Detect Changes in Network Connectivity

Some times you will need a mechanism to check whether changes to network occurring during running your application.
So as a solution for this you can add handlers to the static NetworkAddressChanged and NetworkAvailabilityChanged events implemented by theSystem.Net.NetworkInformation.NetworkChange class.

Souce Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Add the handlers to the NetworkChange events.
            NetworkChange.NetworkAvailabilityChanged +=
            NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged +=
            NetworkAddressChanged;

Console.ReadLine();
        }

// Declare a method to handle NetworkAvailabilityChanged events.
        private static void NetworkAvailabilityChanged(
        object sender, NetworkAvailabilityEventArgs e)
        {
            // Report whether the network is now available or unavailable.
            if (e.IsAvailable)
            {
                Console.WriteLine("Network Available");
            }
            else
            {
                Console.WriteLine("Network Unavailable");
            }
        }
        // Declare a method to handle NetworkAdressChanged events.
        private static void NetworkAddressChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Current IP Addresses:");
            // Iterate through the interfaces and display information.
            foreach (NetworkInterface ni in
            NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation addr
                in ni.GetIPProperties().UnicastAddresses)
                {
                    Console.WriteLine(" - {0} (lease expires {1})",
                    addr.Address, DateTime.Now +
                    new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                }
            }
        }
    }
}


http://www.codeproject.com/Articles/206720/Simple-Network-Status-Monitor-Example

时间: 2024-12-19 16:19:02

Detect Changes in Network Connectivity的相关文章

Linux基本功杂记——[018]——『Network Bonding』

EtherChannel最初是由cisco提出,通过聚合多条物理链路为单条逻辑链路,从而实现高可用及提高吞吐量等目的.AgP(Port Aggregation Protocol,Cisco专有协议).LACP(IEEE 802.3ad)是应用最广泛的两种实现.Linux中的实现称为Bonding,HA的实现需要系统层面Bonding和物理层面switch紧密配合. http://www.mjmwired.net/kernel/Documentation/networking/bonding.tx

[转]Peer-to-Peer Communication Across Network Address Translators

Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of Technology baford (at) mit.edu Pyda Srisuresh Caymas Systems, Inc. srisuresh (at) yahoo.com Dan Kegel dank (at) kegel.com J'fais des trous, des petits

【原创】RabbitMQ 之 Clustering 和 Network Partition(翻译)

Clustering and Network Partitions RabbitMQ clusters do not tolerate network partitions well. If you are thinking of clustering across a WAN, don't. You should use federation or the shovel instead. RabbitMQ 集群无法很好的应对网络分区情况.如果你打算跨 WAN 构建集群,请 别.你应该考虑使用

Android开发训练之第五章——Building Apps with Connectivity & the Cloud

Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to the world beyond the user's device. You'll learn how to connect to other devices in the area, connect to the Internet, backup and sync your app's data, and

Enhancing network controls in mandatory access control computing environments

A?Mandatory?Access?Control?(MAC) aware firewall includes an extended rule set for MAC attributes, such as a security label or path. Application labels may be used to identify processes and perform firewall rule-checking. The firewall rule set may inc

Network Connection Lost When Windows 8 Goes To Sleep

http://www.kapilarya.com/fix-network-connection-lost-when-windows-8-goes-to-sleep http://superuser.com/questions/579405/no-network-connection-after-resume-on-windows-8 If your network connection lost when Windows 8 goes to sleep or hibernate mode, th

Method and Apparatus for Providing Highly-Scalable Network Storage for Well-Gridded Objects

An apparatus comprising a plurality of storage nodes comprising a plurality of corresponding storage disks and configured to store data in a distributed manner between the storage disks that achieves a Redundant Array of Independent Disks-0 (RAID0) l

Network Function Virtualization for a Network Device

An apparatus for performing network function virtualization (NFV), comprising: a memory, a processor coupled to the memory, wherein the memory includes instructions that when executed by the processor cause the apparatus to perform the following: rec

How Network Load Balancing Technology Works--reference

http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balancing Terms and Definitions Network Load Balancing Architecture Network Load Balancing Protocols Application Compatibility with Network Load Balancing