检查网络连接 [英] Check for network connectivity

查看:146
本文介绍了检查网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个简单的应用程序用于侦听<一个href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx"相对=nofollow> NetworkAvailibilityChanged 事件。这一切都很好,除了在调制解调器PPP接口连接,其中应用程序得到通知仅连接在网络,但不是当断开可否订购的情况下。

I am building a simple application that listens to NetworkAvailibilityChanged Event. It's all good, except in the case of a modem PPP interface connection, where the app gets notified of the availibility only when the network is connected but not when disconnected.

我缺少什么?我试了一下局域网,它的工作完美。

Am I missing anything? I tried it for LAN and it worked perfectly.

推荐答案

下面是个什么NetworkAvailbilityChanged触及。

Here is what the NetworkAvailbilityChanged touches.

Friend Shared Function InternalGetIsNetworkAvailable() As Boolean
    If ComNetOS.IsWinNt Then
        Dim interface2 As NetworkInterface
        For Each interface2 In SystemNetworkInterface.GetNetworkInterfaces
            If (((interface2.OperationalStatus = OperationalStatus.Up) AndAlso (interface2.NetworkInterfaceType <> NetworkInterfaceType.Tunnel)) AndAlso (interface2.NetworkInterfaceType <> NetworkInterfaceType.Loopback)) Then
                Return True
            End If
        Next
        Return False
    End If
    Dim flags As UInt32 = 0
    Return UnsafeWinINetNativeMethods.InternetGetConnectedState((flags), 0)
End Function

Private Shared Sub ChangedAddress(ByVal sender As Object, ByVal eventArgs As EventArgs)
    SyncLock AvailabilityChangeListener.syncObject
        Dim isNetworkAvailable As Boolean = SystemNetworkInterface.InternalGetIsNetworkAvailable
        If (isNetworkAvailable <> AvailabilityChangeListener.isAvailable) Then
            AvailabilityChangeListener.isAvailable = isNetworkAvailable
            Dim array As DictionaryEntry() = New DictionaryEntry(AvailabilityChangeListener.s_availabilityCallerArray.Count  - 1) {}
            AvailabilityChangeListener.s_availabilityCallerArray.CopyTo(array, 0)
            Dim i As Integer
            For i = 0 To array.Length - 1
                Dim key As NetworkAvailabilityChangedEventHandler = DirectCast(array(i).Key, NetworkAvailabilityChangedEventHandler)
                Dim context As ExecutionContext = DirectCast(array(i).Value, ExecutionContext)
                If (context Is Nothing) Then
                    key.Invoke(Nothing, New NetworkAvailabilityEventArgs(AvailabilityChangeListener.isAvailable))
                Else
                    ExecutionContext.Run(context.CreateCopy, AvailabilityChangeListener.s_RunHandlerCallback, key)
                End If
            Next i
        End If
    End SyncLock
End Sub

根据这些似乎如果告诉你有网络,也应该告诉你,没有网络。这似乎也工作过的地址为每个设备。当地址发生变化,它会调用ChangedAddress。

According to these it seems that if its telling you there is network, it should also tell you there is not network. It also appears to work off of the Address for each device. When the Address changes, it calls ChangedAddress.

看看这,看看你得到的是怎么回事幕后更清晰的画面。

Take a look at these and see if you get a clearer picture of what is going on behind the scenes.

这篇关于检查网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆