Ping a computer in VB.NET

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
           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

Alas, another abandoned question
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.