【问题标题】:Detect network connectivity from a WPF/XBAP Application?从 WPF/XBAP 应用程序检测网络连接?
【发布时间】:2009-05-13 18:56:46
【问题描述】:

有什么方法可以确定 XBAP(托管在浏览器中的 WPF)应用程序是否具有网络连接性?如果没有,用 C# 和 .NET 编写的传统 Windows 客户端如何确定它是否具有连接性?

基本上,该用例是在通过 WiFi 连接到内部网的移动笔记本电脑上运行的 XBAP 应用程序。笔记本电脑将无法连接到 Internet。 WiFi 连接可能存在也可能不存在,具体取决于用户当时所处的位置。

【问题讨论】:

标签: c# .net wcf xbap


【解决方案1】:

GateWayIPAddressInformation 应该可以工作:http://msdn.microsoft.com/en-us/library/system.net.networkinformation.gatewayipaddressinformation(loband).aspx

如果您获得网关 IP 地址,并且可以 Ping (http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(loband).aspx),则您可能已连接。

【讨论】:

    【解决方案2】:

    在您的 InitializeComponent() 中执行此操作:

    // Add EventHandler for NetworkAddressChanged event    
    NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
    

    这是触发该事件时调用的方法:

    internal void AddressChangedCallback(object sender, EventArgs e)
    {
        // Check for NetworkConnectivity
        _isInternetConnectionActive = new NetworkConnectivity().IsInternetConnected; 
    }
    

    这是 NetworkConnectivity 类:

    public class NetworkConnectivity
    {
        private List<IPAddress> _ipAddresses = new List<IPAddress>();
    
        public NetworkConnectivity()
        {
            _ipAddresses = new List<IPAddress>();
        }
    
        #region Public Properties
        public int CountIPAddresses
        {
            get { return this.IPAddresses.Count; }
        }
        public List<IPAddress> IPAddresses
        {
            get
            {
                _ipAddresses.Clear();
                // Get a listing of all network adapters
                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in adapters)
                {
                    IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
                    GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
                    // If this adapter has at least 1 IPAddress
                    if (addresses.Count > 0)
                    {
                        // Loop through all IP Addresses
                        foreach (GatewayIPAddressInformation address in addresses)
                        {
                            _ipAddresses.Add(address.Address);
                        }
                    }
                } 
                return _ipAddresses;
            }
        }
        public bool IsInternetConnected
        {
            get
            {
                if (this.CountIPAddresses == 0)
                {
                    return false;
                }
                else
                {
                    //IPAddress[] ips = ResolveDNSAddress("google.com");
                    //return PingIPAddressPool(ips);
                    return PingIPAddress("72.14.204.104"); // Google IP
                }
            }
        }
        #endregion
    
        #region Public Methods
        public IPAddress[] ResolveDNSAddress(string UrlAddress)
        {
            IPHostEntry hostInfo = Dns.Resolve(UrlAddress);
            return hostInfo.AddressList;
        }
        public bool PingIPAddressPool(IPAddress[] ipAddresses)
        {
            foreach (IPAddress ip in ipAddresses)
            {
                if (PingIPAddress(ip.Address.ToString()))
                {
                    return true;
                }
            }
            return false;
        }
        public bool PingIPAddress(string ip)
        {
            // Pinging
            IPAddress addr = IPAddress.Parse(ip);
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();
    
            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;
    
            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 15; // seconds to wait for response
            int attempts = 2; // ping attempts
            for (int i = 0; i < attempts; i++)
            {
                PingReply reply = pingSender.Send(addr, timeout, buffer, options); 
                if (reply.Status == IPStatus.Success)
                { return true; }
            }
            return false;
        }
        #endregion
    
    }
    

    【讨论】:

    • 这段代码不错,但是像 Dns.Resolve() 这样的函数已经过时了。
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多