LeeSalter
Active member
Has anybody figured out Ping in vb.Net yet??
Ive looked at some code from another site, but it only seems to work the first time it is run, then comes back as failed every other time, so it cant be trusted.
The code Im on about is posted below:-
Ive looked at some code from another site, but it only seems to work the first time it is run, then comes back as failed every other time, so it cant be trusted.
The code Im on about is posted below:-
Code:
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 net
Private Const portICMP As Integer = 7
Private Const bufferHeaderSize As Integer = 8
Private Const packageHeaderSize As Integer = 28
Public Sub Echo(ByVal RemoteName As String, ByVal DataSize As Byte, ByVal pStatus As Label)
for timing
Dim intStart As Integer
for timing
Dim intEnd As Integer
address/port of remote host
Dim RemoteHost As IPEndPoint
Dim rhEP As EndPoint
id of this packet
Dim Identifier As Short = 0
sequence number of this packet
Dim Sequence As Short = 0
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
RemoteHost = GetRemoteEndpoint(RemoteName)
rhEP = CType(RemoteHost, System.Net.EndPoint)
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
Call CreateChecksum(RequestBuffer, DataSize, RequestBuffer(2), RequestBuffer(3))
Try
ICMPSocket = New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp)
ICMPSocket.Blocking = False
Start Countof RTT
intStart = System.Environment.TickCount
Send Data
ICMPSocket.SendTo(RequestBuffer, 0, DataSize, SocketFlags.None, RemoteHost)
Receive Data
RecvSize = ICMPSocket.ReceiveFrom(ReplyBuffer, SocketFlags.None, rhEP)
End Count of RTT
intEnd = System.Environment.TickCount
If RecvSize > 0 Then
Dim ReS As String
Select Case ReplyBuffer(20)
Case Convert.ToByte(ICMPType.EchoReply)
ReS = (intEnd - intStart).ToString get the RTT
pStatus.Text = "Host: " + RemoteHost.Address.ToString + " - RTT: " + ReS
Case Convert.ToByte(ICMPType.Unreachable)
ReS = "Unreachable"
pStatus.Text = ReS
Case Else
ReS = "Something Happened"
pStatus.Text = ReS
End Select
End If
Catch e As Exception
MessageBox.Show(e.Message + vbNewLine + e.TargetSite.Name)
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 ones
complement of the ones 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