Checking the availability of the VB.net network

I am writing an application that will be used in a mobile environment where the network connection will be regularly updated / updated. How to check if a particular network is available?

+3
source share
3 answers

Try NetworkInterface.GetIsNetworkAvailable:

Indicates if any network connection is available.

A network connection is considered accessible if any network interface is marked up and is not a loopback or tunnel interface.

+5
source

To find out if any network is available, you can use the VB.NET My namespace:

My.Computer.Network.IsAvailable

, , NetworkInterface . , ,

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

I like to dig old threads! My solution was to check the DNS. This way you can test certain names inside the xxx network to see if you are inside / outside. Nested try statements show this concept.

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
0
source

Source: https://habr.com/ru/post/1726353/


All Articles