Ping a computer in VB.NET

Posted on 2004-11-14
Last Modified: 2012-08-14
Hello everyone
im looking for some good code for pinging a computer on the network in VB.NET

i have found some code that works in 9 out of 10 times but i need code that works 10 out of 10 times, i dont have to get responsetime and so one, all that i want i a boolean variable saying if the ipadress was found on the network or not

Question by:haggmar
    LVL 1

    Accepted Solution

    You can try to create a Process, set up the input stream, and the output stream. the process will be a ping, or even the inputing the ping command. Then all the output will be like a real ping we are used too. Then you can get all you need.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Here is the Microsoft Example:;en-us;828993

    Also check out this article:


    VB.NET  •  Emulate Ping Functionality

    Listing 2. Here's how to create a socket for an ICMP Echo message, fill Echo message data, send a message, and retrieve a response.

    Option Strict On
    Option Explicit On

    Imports System.Net
    Imports System.Net.Sockets

    Public Enum ICMPType
       EchoReply = 0
       Unreachable = 3
       Echo = 8
    End Enum

    ' --- ICMP Echo Header Format ---
    ' (first 8 bytes of the data buffer)

    ' Buffer (0) ICMP Type Field
    ' Buffer (1) ICMP Code Field
    '     (must be 0 for Echo and Echo Reply)
    ' Buffer (2) checksum hi
    '     (must be 0 before checksum calc)
    ' Buffer (3) checksum lo
    '     (must be 0 before checksum calc)
    ' Buffer (4) ID hi
    ' Buffer (5) ID lo
    ' Buffer (6) sequence hi
    ' Buffer (7) sequence lo
    ' Buffer (8)..(n)  Ping Data

    Module Module1
       Private Const portICMP As Integer = 7
       Private Const bufferHeaderSize As Integer = 8
       Private Const packageHeaderSize As Integer _
          = 28

       Sub Main()
          Dim hostName As String
          Console.Write( _
             "Enter Remote Host Name or IP: ")
          hostName = Console.ReadLine()
          Console.WriteLine("Press 'Enter' to exit.")
       End Sub

       Public Sub Echo(ByVal RemoteName As String)
          'address/port of remote host
          Dim RemoteHost As IPEndPoint

          'id of this packet
          Dim Identifier As Short = 0

          'sequence number of this packet
          Dim Sequence As Short = 0

          'number of bytes of data to send
          Dim DataSize As Byte = 32

          'the socket we use to connect and
          'send data through
          Dim ICMPSocket As Socket

          'the request buffer
          Dim RequestBuffer() As Byte

          'the reply buffer
          Dim ReplyBuffer(255) As Byte

          'the number of bytes received
          Dim RecvSize As Integer = 0

             ICMPSocket = New _
                Socket(AddressFamily.InterNetwork, _
                SocketType.Raw, ProtocolType.Icmp)

             ICMPSocket.Blocking = False

             RemoteHost = _

             DataSize = Convert.ToByte(DataSize + _

             ' If odd data size, we need to add
             ' one empty byte
             If (DataSize Mod 2 = 1) Then
                DataSize += Convert.ToByte(1)
             End If
             ReDim RequestBuffer(DataSize - 1)

             ' Set Type Field
             RequestBuffer(0) = _
             ' Set ID Field
             BitConverter.GetBytes( _
                Identifier).CopyTo(RequestBuffer, 4)
             ' Set Sequence Field
             BitConverter.GetBytes( _
                Sequence).CopyTo(RequestBuffer, 6)

             ' load some data into buffer
             Dim i As Integer
             For i = 8 To DataSize - 1
                RequestBuffer(i) = _
                Convert.ToByte(i Mod 8)
             Next i

             ' Set Checksum
             CreateChecksum(RequestBuffer, _
                DataSize, RequestBuffer(2), _

             ICMPSocket.SendTo(RequestBuffer, 0, _
                DataSize, SocketFlags.None, _

             RecvSize = ICMPSocket.ReceiveFrom( _
                ReplyBuffer, SocketFlags.None, _
                CType(RemoteHost, EndPoint))

             If RecvSize > 0 Then
                Select Case ReplyBuffer(20)
                   Case Convert.ToByte( _
                      Console.WriteLine( _
                         "Remote host " + _
                         RemoteHost.Address. _
                         ToString _
                         + " responded. " + _
                         (RecvSize - _
                         packageHeaderSize _
                         ).ToString() + _
                         " bytes received.")
                   Case Convert.ToByte( _
                      Console.WriteLine( _
                         "Remote endpoint " + _
                   Case Else
                      Console.WriteLine( _
                         "Received unexpected " _
                         + "data...")
                   End Select
                End If

          Catch e As Exception
             Console.WriteLine("Error: " + _
             If Not ICMPSocket Is Nothing Then
             End If
          End Try
       End Sub

       Public Function GetRemoteEndpoint(ByVal _
          RemoteAddress As String) As IPEndPoint
          Return New IPEndPoint( _
             Dns.Resolve( _
             RemoteAddress).AddressList(0) _
             , portICMP)
       End Function

       ' ICMP requires a checksum that is the one's
       ' complement of the one's complement sum of
       ' all the 16-bit values in the data in the
       ' buffer.
       ' Use this procedure to load the Checksum
       ' field of the buffer.
       ' The Checksum Field (hi and lo bytes) must be
       ' zero before calling this procedure.
       Private Sub CreateChecksum(ByRef data() As _
          Byte, ByVal Size As Integer, ByRef HiByte _
          As Byte, ByRef LoByte As Byte)
          Dim i As Integer
          Dim chk As Integer = 0

          For i = 0 To Size - 1 Step 2
             chk += Convert.ToInt32(data(i) * _
                &H100 + data(i + 1))

          chk = Convert.ToInt32((chk And &HFFFF&) + _
             Fix(chk / &H10000&))
          chk += Convert.ToInt32(Fix(chk / &H10000&))
          chk = Not (chk)

          HiByte = Convert.ToByte((chk And &HFF00) _
             / &H100)
          LoByte = Convert.ToByte(chk And &HFF)
       End Sub
    End Module
    LVL 5

    Assisted Solution

               Dim si As New System.Diagnostics.ProcessStartInfo("cmd.exe")
                Dim p As Process
                Dim x As String
                Dim counter As Integer = 700            'you may need to play with this number to get it right for your system
                Dim y(counter) As Char
                Dim i As Integer
                      si.RedirectStandardInput = True
                      si.RedirectStandardOutput = True
                      si.UseShellExecute = False
                      si.CreateNoWindow = True
                      p = Process.Start(si)
                      p.StandardOutput.ReadBlock(y, 0, counter)
                      For i = 0 To counter - 1
                            x &= y(i).ToString
                Catch ioe As InvalidOperationException
                      'this will throw invalidexception when done
                Catch ex As Exception

                      'now just parse what you want out of x

                End Try

    LVL 5

    Expert Comment

    Alas, another abandoned question

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now