VB Dot NET Forum: Ping Test Class - VB Dot NET Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ping Test Class Allows the validation of an IP & Host Availability

#1 User is offline   Brian Icon

  • Member
  • PipPip
Group:
Members
Posts:
21
Joined:
25-March 09

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Windows XP

Posted 27 March 2009 - 04:45 AM

Use IsValidIP function to determine if an IP address is valid. Returns Boolean.
Use HostAvailable to ping an IP. Returns TestResult structure.

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Text

Public Class PingTest
    Public Structure TestResult
        Dim IsAvailable As Boolean
        Dim ErrorMsg As String
        Dim Ex As Exception
    End Structure


    Public Function HostAvailable(ByVal IPAddress As String) As TestResult
        Dim result As New TestResult

        If Not IsValidIP(IPAddress) Then
            result.IsAvailable = False
            result.ErrorMsg = "Invalid IP Address"
            Return result
            Exit Function
        End If

        Dim pingSender As Ping = New Ping
        Dim pingOptions As PingOptions = New PingOptions
        pingOptions.DontFragment = True
        Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        Dim buffer() As Byte = Encoding.ASCII.GetBytes(data)
        Dim timeOut As Integer = 150
        Dim reply As PingReply
        Try
            reply = pingSender.Send(IPAddress, timeOut, buffer, pingOptions)
            If reply.Status = IPStatus.Success Then
                result.IsAvailable = True
            Else
                result.IsAvailable = False
                result.ErrorMsg = reply.Status.ToString
            End If
            Return result
        Catch ex As Exception
            result.IsAvailable = False
            result.ErrorMsg = ""
            result.Ex = ex
            Return result
        End Try

    End Function

    Function IsValidIP(ByVal IP As String) As Boolean
        'Check for zero length
        If IP.Length = 0 Then
            Return False
        End If

        Dim IPx As IPAddress = Nothing
        Dim valid As Boolean = System.Net.IPAddress.TryParse(IP, IPx)
        If valid Then
            'Just because IP can be parsed, it doesn't mean it is a valid IP Address
            'TryParse does a great job of coercing a value into a valid IP address,
            'so compare the input IP to the result of the TryParse.
            If valid And IPx.ToString.CompareTo(IP) = 0 Then
                IsValidIP = True
            End If
            IPx = Nothing
        Else
            Return False
        End If
    End Function
End Class

0

#2 User is offline   sharimila Icon

  • Newbie
  • Pip
Group:
Members
Posts:
2
Joined:
21-July 10

VB Knowledge: None
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Windows 7

Posted 21 July 2010 - 06:29 AM

I test the ping test here http://www.whoisxy.com/ and also i got the whois information,ip to domain,domain to ip,etc.....
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users