R
Rob Snell
Hi all,
I hope this is the correct group, if not, please let me know. I am
trying to implement ping. I found some excellent code that builds a
raw socket from scratch and it works great against localhost but
nothing external. The complete code is below but I seem to timeout
trying to receive bytes. When I look to see how many bytes I have in
the buffer, I don't have any.
Any ideas would be greatly appreciated.
'the form
Option Explicit On
Public Class TestPing
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.RichTextBox1.Location = New System.Drawing.Point(8, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(584, 416)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 432)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(232, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Ping"
'
'TestPing
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(600, 462)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "TestPing"
Me.Text = "TestPing"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub TestPing()
Try
Me.Show()
Application.DoEvents()
RichTextBox1.Clear()
Dim vChk As String
Dim vHost As String
For ChkLoop As Integer = 0 To 2
Select Case ChkLoop
Case 0
vHost = "localhost"
Case 1
vHost = "yahoo.com"
Case 2
vHost = "192.168.10.110"
Case 3
vHost = "www.google.com"
Case 4
vHost = "www.znet.com"
Case 5
vHost = "www.google.com"
Case 6
vHost = "localhost"
Case 7
vHost = "www.compaq.com"
End Select
Try
Dim cPing As ClsPing
cPing = New ClsPing
cPing.Host = (vHost)
vChk = CStr(cPing.Ping(RichTextBox1))
RichTextBox1.AppendText(vHost & ": ticks taken:" &
vChk & ControlChars.NewLine)
Application.DoEvents()
Catch ex As Exception
If vChk = -2 Then
RichTextBox1.AppendText(vHost & ": unable to
ping server." & ControlChars.NewLine)
End If
RichTextBox1.AppendText(ex.ToString() &
ControlChars.NewLine)
Application.DoEvents()
End Try
Next
Catch ex As Exception
RichTextBox1.AppendText(Err.Description &
ControlChars.NewLine)
Application.DoEvents()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
TestPing()
End Sub
End Class
'the class
Option Explicit On
Imports System
Imports System.Net
Imports System.Net.Sockets
Public Class ClsPing
Public Structure stcError
Dim Number As Integer
Dim Description As String
End Structure
Public Const PING_SUCCESS As Long = 0
Public Const PING_ERROR As Long = (-1)
Public Const PING_ERROR_BASE As Long = &H8000
Public Const PING_ERROR_HOST_NOT_FOUND As Long = PING_ERROR_BASE +
1
Public Const PING_ERROR_SOCKET_DIDNT_SEND As Long =
PING_ERROR_BASE + 2
Public Const PING_ERROR_HOST_NOT_RESPONDING As Long =
PING_ERROR_BASE + 3
Public Const PING_ERROR_TIME_OUT As Long = PING_ERROR_BASE + 4
Private Const ICMPType_ECHO As Integer = 8
Private Const SOCKET_ERROR As Integer = -1
Private udtError As stcError
Private Const intPortICMP As Integer = 7
Private Const intBufferHeaderSize As Integer = 8
Private Const intPackageHeaderSize As Integer = 28
Private intDataSize As Byte
Private ipheLocalHost As System.Net.IPHostEntry
Private ipheHost As System.Net.IPHostEntry
Public Property DataSize() As Byte
Get
Return intDataSize
End Get
Set(ByVal Value As Byte)
intDataSize = Value
End Set
End Property
Public ReadOnly Property Identifier() As Byte
Get
Return 0
End Get
End Property
Public ReadOnly Property Sequence() As Byte
Get
Return 0
End Get
End Property
Public ReadOnly Property LocalHost() As System.Net.IPHostEntry
Get
Return ipheLocalHost
End Get
End Property
Public Property Host() As Object
Get
Return ipheHost
End Get
Set(ByVal Value As Object)
If (Value.GetType.ToString.ToLower = "system.string") Then
'*
'* Find the Host's IP address
'*
Try
ipheHost = System.Net.Dns.GetHostByName(Value)
'MsgBox(System.Net.Dns.GetHostByName(Value))
Catch
ipheHost = Nothing
udtError.Number = PING_ERROR_HOST_NOT_FOUND
udtError.Description = "Host " & ipheHost.HostName
& " not found."
End Try
ElseIf (Value.GetType.ToString.ToLower =
"system.net.iphostentry") Then
ipheHost = (Value)
Else
ipheHost = Nothing
End If
End Set
End Property
'*
'* Class Constructor
'*
Public Sub New()
'*
'* Initializes the parameters
'*
intDataSize = 32
udtError = New stcError
'*
'* Get local IP and transform in EndPoint
'*
ipheLocalHost =
System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
'*
'* Defines Host
'*
ipheHost = Nothing
End Sub
'*
'* Function : PingHost
'* Author : Paulo dos Santos Silva Jr
'* Date : 05/09/2002
'* Objective : Pings a specified host
'*
'* Parameters : Host as String
'*
'* Returns : Response time in milliseconds
'* (-1) if error
'*
Public Function Ping(ByVal rtb As RichTextBox) As Long
Dim intCount As Integer
Dim aReplyBuffer(255) As Byte
Dim intNBytes As Integer = 0
Dim intEnd As Integer
Dim intStart As Integer
Dim epFrom As System.Net.EndPoint
Dim epServer As System.Net.EndPoint
Dim ipepServer As System.Net.IPEndPoint
Dim sckSocket As System.Net.Sockets.Socket
'*
'* Transforms the IP address in EndPoint
'*
ipepServer = New
System.Net.IPEndPoint(ipheHost.AddressList(0), 0)
epServer = CType(ipepServer, System.Net.EndPoint)
epFrom = New
System.Net.IPEndPoint(ipheLocalHost.AddressList(0), 0)
'*
'* Builds the packet to send
'*
DataSize = Convert.ToByte(DataSize + intBufferHeaderSize)
'*
'* The packet must by an even number, so if the DataSize is
and odd number adds one
'*
If (DataSize Mod 2 = 1) Then
DataSize += Convert.ToByte(1)
End If
Dim aRequestBuffer(DataSize - 1) As Byte
'*
'* --- 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
'*
'*
'* Set Type Field
'*
aRequestBuffer(0) = Convert.ToByte(ICMPType_ECHO)
'*
'* Set ID field
'*
BitConverter.GetBytes(Identifier).CopyTo(aRequestBuffer, 4)
'*
'* Set Sequence
'*
BitConverter.GetBytes(Sequence).CopyTo(aRequestBuffer, 6)
'*
'* Load some data into buffer
'*
Dim i As Integer
For i = 8 To DataSize - 1
aRequestBuffer(i) = Convert.ToByte(i Mod 8)
Next i
'*
'* Calculate Checksum
'*
Call CreateChecksum(aRequestBuffer, DataSize,
aRequestBuffer(2), aRequestBuffer(3))
'*
'* Try send the packet
'*
Try
'*
'* Create the socket
'*
sckSocket = New System.Net.Sockets.Socket( _
Net.Sockets.AddressFamily.InterNetwork, _
Net.Sockets.SocketType.Raw, _
Net.Sockets.ProtocolType.Icmp)
sckSocket.Blocking = False
'rjs
sckSocket.Bind(epFrom)
'*
'* Records the Start Time
'*
intStart = System.Environment.TickCount
sckSocket.SendTo(aRequestBuffer, 0, DataSize,
SocketFlags.None, ipepServer)
Dim bTimedOut As Boolean = False
Dim iMaxTicks As Integer = 30 * 1000
'use Available instead of all this
While (Not bTimedOut) And (intNBytes = 0)
Try
intNBytes = sckSocket.ReceiveFrom(aReplyBuffer,
SocketFlags.Peek, epServer)
Catch ex As SocketException
If ex.ErrorCode <> 10035 Then
Throw ex
End If
End Try
If System.Environment.TickCount >= (intStart +
iMaxTicks) Then
bTimedOut = True
rtb.AppendText("Timeout. Start:" & intStart & "
Timeout@" & intStart + iMaxTicks & " Now:" &
System.Environment.TickCount & ControlChars.NewLine)
End If
End While
rtb.AppendText("Peek: " & intNBytes.ToString & " bytes" &
ControlChars.NewLine)
If Not bTimedOut Then
intNBytes = sckSocket.ReceiveFrom(aReplyBuffer,
SocketFlags.None, epServer)
End If
intEnd = System.Environment.TickCount
If (intNBytes > 0) Then
'*
'* Informs on GetLastError the state of the server
'*
udtError.Number = (aReplyBuffer(19) * &H100) +
aReplyBuffer(20)
Select Case aReplyBuffer(20)
Case 0 : udtError.Description = "Success"
Case 1 : udtError.Description = "Buffer too Small"
Case 2 : udtError.Description = "Destination
Unreahable"
Case 3 : udtError.Description = "Dest Host Not
Reachable"
Case 4 : udtError.Description = "Dest Protocol Not
Reachable"
Case 5 : udtError.Description = "Dest Port Not
Reachable"
Case 6 : udtError.Description = "No Resources
Available"
Case 7 : udtError.Description = "Bad Option"
Case 8 : udtError.Description = "Hardware Error"
Case 9 : udtError.Description = "Packet too Big"
Case 10 : udtError.Description = "Reqested Timed
Out"
Case 11 : udtError.Description = "Bad Request"
Case 12 : udtError.Description = "Bad Route"
Case 13 : udtError.Description = "TTL Exprd In
Transit"
Case 14 : udtError.Description = "TTL Exprd
Reassemb"
Case 15 : udtError.Description = "Parameter
Problem"
Case 16 : udtError.Description = "Source Quench"
Case 17 : udtError.Description = "Option too Big"
Case 18 : udtError.Description = "Bad Destination"
Case 19 : udtError.Description = "Address Deleted"
Case 20 : udtError.Description = "Spec MTU Change"
Case 21 : udtError.Description = "MTU Change"
Case 22 : udtError.Description = "Unload"
Case Else : udtError.Description = "General
Failure"
End Select
End If
sckSocket.Close()
Return (intEnd - intStart)
Catch oExcept As Exception
rtb.AppendText(oExcept.ToString())
sckSocket.Close()
Return -2
End Try
End Function
Public Function GetLastError() As stcError
Return udtError
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 Class
I hope this is the correct group, if not, please let me know. I am
trying to implement ping. I found some excellent code that builds a
raw socket from scratch and it works great against localhost but
nothing external. The complete code is below but I seem to timeout
trying to receive bytes. When I look to see how many bytes I have in
the buffer, I don't have any.
Any ideas would be greatly appreciated.
'the form
Option Explicit On
Public Class TestPing
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.RichTextBox1.Location = New System.Drawing.Point(8, 8)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(584, 416)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 432)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(232, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Ping"
'
'TestPing
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(600, 462)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "TestPing"
Me.Text = "TestPing"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub TestPing()
Try
Me.Show()
Application.DoEvents()
RichTextBox1.Clear()
Dim vChk As String
Dim vHost As String
For ChkLoop As Integer = 0 To 2
Select Case ChkLoop
Case 0
vHost = "localhost"
Case 1
vHost = "yahoo.com"
Case 2
vHost = "192.168.10.110"
Case 3
vHost = "www.google.com"
Case 4
vHost = "www.znet.com"
Case 5
vHost = "www.google.com"
Case 6
vHost = "localhost"
Case 7
vHost = "www.compaq.com"
End Select
Try
Dim cPing As ClsPing
cPing = New ClsPing
cPing.Host = (vHost)
vChk = CStr(cPing.Ping(RichTextBox1))
RichTextBox1.AppendText(vHost & ": ticks taken:" &
vChk & ControlChars.NewLine)
Application.DoEvents()
Catch ex As Exception
If vChk = -2 Then
RichTextBox1.AppendText(vHost & ": unable to
ping server." & ControlChars.NewLine)
End If
RichTextBox1.AppendText(ex.ToString() &
ControlChars.NewLine)
Application.DoEvents()
End Try
Next
Catch ex As Exception
RichTextBox1.AppendText(Err.Description &
ControlChars.NewLine)
Application.DoEvents()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
TestPing()
End Sub
End Class
'the class
Option Explicit On
Imports System
Imports System.Net
Imports System.Net.Sockets
Public Class ClsPing
Public Structure stcError
Dim Number As Integer
Dim Description As String
End Structure
Public Const PING_SUCCESS As Long = 0
Public Const PING_ERROR As Long = (-1)
Public Const PING_ERROR_BASE As Long = &H8000
Public Const PING_ERROR_HOST_NOT_FOUND As Long = PING_ERROR_BASE +
1
Public Const PING_ERROR_SOCKET_DIDNT_SEND As Long =
PING_ERROR_BASE + 2
Public Const PING_ERROR_HOST_NOT_RESPONDING As Long =
PING_ERROR_BASE + 3
Public Const PING_ERROR_TIME_OUT As Long = PING_ERROR_BASE + 4
Private Const ICMPType_ECHO As Integer = 8
Private Const SOCKET_ERROR As Integer = -1
Private udtError As stcError
Private Const intPortICMP As Integer = 7
Private Const intBufferHeaderSize As Integer = 8
Private Const intPackageHeaderSize As Integer = 28
Private intDataSize As Byte
Private ipheLocalHost As System.Net.IPHostEntry
Private ipheHost As System.Net.IPHostEntry
Public Property DataSize() As Byte
Get
Return intDataSize
End Get
Set(ByVal Value As Byte)
intDataSize = Value
End Set
End Property
Public ReadOnly Property Identifier() As Byte
Get
Return 0
End Get
End Property
Public ReadOnly Property Sequence() As Byte
Get
Return 0
End Get
End Property
Public ReadOnly Property LocalHost() As System.Net.IPHostEntry
Get
Return ipheLocalHost
End Get
End Property
Public Property Host() As Object
Get
Return ipheHost
End Get
Set(ByVal Value As Object)
If (Value.GetType.ToString.ToLower = "system.string") Then
'*
'* Find the Host's IP address
'*
Try
ipheHost = System.Net.Dns.GetHostByName(Value)
'MsgBox(System.Net.Dns.GetHostByName(Value))
Catch
ipheHost = Nothing
udtError.Number = PING_ERROR_HOST_NOT_FOUND
udtError.Description = "Host " & ipheHost.HostName
& " not found."
End Try
ElseIf (Value.GetType.ToString.ToLower =
"system.net.iphostentry") Then
ipheHost = (Value)
Else
ipheHost = Nothing
End If
End Set
End Property
'*
'* Class Constructor
'*
Public Sub New()
'*
'* Initializes the parameters
'*
intDataSize = 32
udtError = New stcError
'*
'* Get local IP and transform in EndPoint
'*
ipheLocalHost =
System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
'*
'* Defines Host
'*
ipheHost = Nothing
End Sub
'*
'* Function : PingHost
'* Author : Paulo dos Santos Silva Jr
'* Date : 05/09/2002
'* Objective : Pings a specified host
'*
'* Parameters : Host as String
'*
'* Returns : Response time in milliseconds
'* (-1) if error
'*
Public Function Ping(ByVal rtb As RichTextBox) As Long
Dim intCount As Integer
Dim aReplyBuffer(255) As Byte
Dim intNBytes As Integer = 0
Dim intEnd As Integer
Dim intStart As Integer
Dim epFrom As System.Net.EndPoint
Dim epServer As System.Net.EndPoint
Dim ipepServer As System.Net.IPEndPoint
Dim sckSocket As System.Net.Sockets.Socket
'*
'* Transforms the IP address in EndPoint
'*
ipepServer = New
System.Net.IPEndPoint(ipheHost.AddressList(0), 0)
epServer = CType(ipepServer, System.Net.EndPoint)
epFrom = New
System.Net.IPEndPoint(ipheLocalHost.AddressList(0), 0)
'*
'* Builds the packet to send
'*
DataSize = Convert.ToByte(DataSize + intBufferHeaderSize)
'*
'* The packet must by an even number, so if the DataSize is
and odd number adds one
'*
If (DataSize Mod 2 = 1) Then
DataSize += Convert.ToByte(1)
End If
Dim aRequestBuffer(DataSize - 1) As Byte
'*
'* --- 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
'*
'*
'* Set Type Field
'*
aRequestBuffer(0) = Convert.ToByte(ICMPType_ECHO)
'*
'* Set ID field
'*
BitConverter.GetBytes(Identifier).CopyTo(aRequestBuffer, 4)
'*
'* Set Sequence
'*
BitConverter.GetBytes(Sequence).CopyTo(aRequestBuffer, 6)
'*
'* Load some data into buffer
'*
Dim i As Integer
For i = 8 To DataSize - 1
aRequestBuffer(i) = Convert.ToByte(i Mod 8)
Next i
'*
'* Calculate Checksum
'*
Call CreateChecksum(aRequestBuffer, DataSize,
aRequestBuffer(2), aRequestBuffer(3))
'*
'* Try send the packet
'*
Try
'*
'* Create the socket
'*
sckSocket = New System.Net.Sockets.Socket( _
Net.Sockets.AddressFamily.InterNetwork, _
Net.Sockets.SocketType.Raw, _
Net.Sockets.ProtocolType.Icmp)
sckSocket.Blocking = False
'rjs
sckSocket.Bind(epFrom)
'*
'* Records the Start Time
'*
intStart = System.Environment.TickCount
sckSocket.SendTo(aRequestBuffer, 0, DataSize,
SocketFlags.None, ipepServer)
Dim bTimedOut As Boolean = False
Dim iMaxTicks As Integer = 30 * 1000
'use Available instead of all this
While (Not bTimedOut) And (intNBytes = 0)
Try
intNBytes = sckSocket.ReceiveFrom(aReplyBuffer,
SocketFlags.Peek, epServer)
Catch ex As SocketException
If ex.ErrorCode <> 10035 Then
Throw ex
End If
End Try
If System.Environment.TickCount >= (intStart +
iMaxTicks) Then
bTimedOut = True
rtb.AppendText("Timeout. Start:" & intStart & "
Timeout@" & intStart + iMaxTicks & " Now:" &
System.Environment.TickCount & ControlChars.NewLine)
End If
End While
rtb.AppendText("Peek: " & intNBytes.ToString & " bytes" &
ControlChars.NewLine)
If Not bTimedOut Then
intNBytes = sckSocket.ReceiveFrom(aReplyBuffer,
SocketFlags.None, epServer)
End If
intEnd = System.Environment.TickCount
If (intNBytes > 0) Then
'*
'* Informs on GetLastError the state of the server
'*
udtError.Number = (aReplyBuffer(19) * &H100) +
aReplyBuffer(20)
Select Case aReplyBuffer(20)
Case 0 : udtError.Description = "Success"
Case 1 : udtError.Description = "Buffer too Small"
Case 2 : udtError.Description = "Destination
Unreahable"
Case 3 : udtError.Description = "Dest Host Not
Reachable"
Case 4 : udtError.Description = "Dest Protocol Not
Reachable"
Case 5 : udtError.Description = "Dest Port Not
Reachable"
Case 6 : udtError.Description = "No Resources
Available"
Case 7 : udtError.Description = "Bad Option"
Case 8 : udtError.Description = "Hardware Error"
Case 9 : udtError.Description = "Packet too Big"
Case 10 : udtError.Description = "Reqested Timed
Out"
Case 11 : udtError.Description = "Bad Request"
Case 12 : udtError.Description = "Bad Route"
Case 13 : udtError.Description = "TTL Exprd In
Transit"
Case 14 : udtError.Description = "TTL Exprd
Reassemb"
Case 15 : udtError.Description = "Parameter
Problem"
Case 16 : udtError.Description = "Source Quench"
Case 17 : udtError.Description = "Option too Big"
Case 18 : udtError.Description = "Bad Destination"
Case 19 : udtError.Description = "Address Deleted"
Case 20 : udtError.Description = "Spec MTU Change"
Case 21 : udtError.Description = "MTU Change"
Case 22 : udtError.Description = "Unload"
Case Else : udtError.Description = "General
Failure"
End Select
End If
sckSocket.Close()
Return (intEnd - intStart)
Catch oExcept As Exception
rtb.AppendText(oExcept.ToString())
sckSocket.Close()
Return -2
End Try
End Function
Public Function GetLastError() As stcError
Return udtError
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 Class