【问题标题】:Check Network Availablility VB.net检查网络可用性 VB.net
【发布时间】:2009-12-22 15:53:57
【问题描述】:

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

【问题讨论】:

    标签: .net vb.net networking


    【解决方案1】:

    试试NetworkInterface.GetIsNetworkAvailable:

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

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

    【讨论】:

    • 有更快的方法吗?这个特定的调用速度相对较慢,并导致我正在开发的移动应用程序出现性能问题。 (我将它设计为在每次交互之前运行——所以它运行得很多)我的设备有两个网络设备——如果我能找到一种方法来检查我关心的一个适配器,至少理论上它会是 50%快点。有人知道怎么做吗?
    【解决方案2】:

    要查看是否有可用的网络,您可以使用 VB.NET 我的命名空间:

    My.Computer.Network.IsAvailable
    

    我猜这是 Andrew 回答中 NetworkInterface 属性的抽象。要查看是否可以使用可用网络访问特定服务器,可以使用

    My.Computer.Network.Ping(host name or IP address, or a System.Uri)
    

    【讨论】:

      【解决方案3】:

      我喜欢挖掘旧线程!我的解决方案是使用 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
      

      【讨论】:

        猜你喜欢
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多