Jeff
Jeff

Reputation: 8138

Check Network Availablility VB.net

I'm writing an application that will be used in a mobile environment where network connectivity will be up/down regularly. How do I check to see that a specific network is available?

Upvotes: 3

Views: 21263

Answers (3)

akuma6099
akuma6099

Reputation: 1

I love digging up old threads! My solution was to test with DNS. This way you can test for specific names inside xxx network to tell if you are inside/outside. The nested try statements shows 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

Upvotes: 0

PhilPursglove
PhilPursglove

Reputation: 12589

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

My.Computer.Network.IsAvailable

which I would guess is an abstraction of the NetworkInterface property in Andrew's answer. To see if you can use an available network to get to a specific server, you can use

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

Upvotes: 4

Andrew Hare
Andrew Hare

Reputation: 351456

Try NetworkInterface.GetIsNetworkAvailable:

Indicates whether any network connection is available.

A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.

Upvotes: 5

Related Questions