代码之家  ›  专栏  ›  技术社区  ›  Jeff

检查网络可用性vb.net

  •  3
  • Jeff  · 技术社区  · 16 年前

    我正在编写一个应用程序,该应用程序将在移动环境中使用,在该环境中,网络连接将定期打开/关闭。如何检查特定网络是否可用?

    3 回复  |  直到 12 年前
        1
  •  5
  •   Andrew Hare    16 年前

    尝试 NetworkInterface.GetIsNetworkAvailable :

    指示是否有可用的网络连接。

    如果任何网络接口标记为“up”,并且不是环回或隧道接口,则认为网络连接可用。

        2
  •  3
  •   PhilPursglove    16 年前

    要查看是否有可用的网络,可以使用vb.net my命名空间:

    My.Computer.Network.IsAvailable
    

    我想这是对 NetworkInterface 安德鲁答案中的属性。要查看是否可以使用可用网络访问特定服务器,可以使用

    My.Computer.Network.Ping(host name or IP address, or a System.Uri)
    
        3
  •  0
  •   akuma6099    12 年前

    我喜欢挖旧线!我的解决方案是用DNS进行测试。通过这种方式,您可以测试xxx网络中的特定名称,以判断您是否在内部/外部。嵌套的try语句显示了这个概念。

    Imports System.Net
    
    Module Networker
    
    Dim Online_Status As Boolean = vbFalse
    Dim InsideJoeNetwork As Boolean = vbFalse
    Dim CurrentJoeIPAddress As New IPHostEntry
    
    
    
    Public ReadOnly Property GetOnlineStatus() As String
        Get
            Return Online_Status
        End Get
    
    End Property
    
    
    
    Public ReadOnly Property InsideJoeNet() As String
        Get
            Return InsideJoeNetwork
        End Get
    
    End Property
    
    
    
    Sub Initialize()
        Set_Online_Status()
    
    End Sub
    
    
    
    Public Sub Set_Online_Status()
    
        If My.Computer.Network.IsAvailable Then
            Try
                Dim DNSTest As IPHostEntry = Dns.GetHostEntry("google.com")
                If DNSTest.AddressList.Length > 0 Then
                    Online_Status = True
                    Detect_Joe_Network()
                Else : Online_Status = False
    
                End If
    
            Catch ex As System.Net.Sockets.SocketException
    
                Online_Status = False
    
            End Try
        End If
    
    End Sub
    
    
    
    Public Sub Detect_Joe_Network()
    
        If Online_Status = True Then
    
            Dim JoeIP As IPHostEntry = New IPHostEntry()
    
            Try
                JoeIP = Dns.GetHostEntry("laptop")
                If JoeIP.AddressList.Length > 0 Then
    
                    InsideJoeNetwork = True
                    CurrentJoeIPAddress = JoeIP
                    'MessageBox.Show(JoeIP.HostName.ToString(), "JoeIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Catch ex As Sockets.SocketException
    
                Try
                    JoeIP = Dns.GetHostEntry("laptop.exampledomain.com")
                    If JoeIP.AddressList.Length > 0 Then
    
                        InsideJoeNetwork = False
                        CurrentJoeIPAddress = JoeIP
                        ' MessageBox.Show(JoeIP.HostName.ToString(), "JoeIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Catch ey As Sockets.SocketException
    
                End Try
            End Try
        End If
    
    End Sub
    
    End Module