[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1163
  • Last Modified:

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


martin
0
haggmar
Asked:
haggmar
  • 2
3 Solutions
 
AlexmipegoCommented:
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 command.com 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is the Microsoft Example:
http://support.microsoft.com/default.aspx?scid=kb;en-us;828993

Also check out this article: http://www.fawcette.com/vsm/2002_03/magazine/columns/qa/page2.aspx

From: http://www.fawcette.com/vsm/2002_03/magazine/columns/qa/list2.aspx

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()
      Echo(hostName)
      Console.WriteLine("Press 'Enter' to exit.")
      Console.ReadLine()
   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

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

         ICMPSocket.Blocking = False

         RemoteHost = _
            GetRemoteEndpoint(RemoteName)

         DataSize = Convert.ToByte(DataSize + _
            bufferHeaderSize)

         ' 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) = _
         Convert.ToByte(ICMPType.Echo)
         ' 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), _
            RequestBuffer(3))

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

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

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

      Catch e As Exception
         Console.WriteLine("Error: " + _
            e.Message)
      Finally
         If Not ICMPSocket Is Nothing Then
            ICMPSocket.Close()
         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))
      Next

      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
0
 
TRUENEUTRALCommented:
           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
            Try
                  si.RedirectStandardInput = True
                  si.RedirectStandardOutput = True
                  si.UseShellExecute = False
                  si.CreateNoWindow = True
                  p = Process.Start(si)
                  p.StandardInput.WriteLine("ping 10.1.10.1")
                  p.StandardOutput.ReadBlock(y, 0, counter)
                  For i = 0 To counter - 1
                        x &= y(i).ToString
                  Next
                  p.StandardInput.WriteLine("exit")
                  p.Kill()
            Catch ioe As InvalidOperationException
                  'this will throw invalidexception when done
            Catch ex As Exception
                  MsgBox(ex.ToString)

            Finally
                  'now just parse what you want out of x
                  MsgBox(x)

            End Try

0
 
TRUENEUTRALCommented:
Alas, another abandoned question
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now