Ok,

I was browsing the board when I found a section on how to preform a ping for vb.net. The only problem is that it is a module form and I will be needing it in a class.

Attatched is what I have done thus far, nothing more than putting it in a class and (in my opinion) cleaning it up by taking out the _ for the continue lines. The only problem is that I can't get it to run, even if I add the class as a file in the program instead of a reference. I am invoking it by delcaring a variable to the class then saying something like textbox2.text = variable.echo(textbox1.text) but I get this:

An unhandled exception of type 'System.NullReferenceException' occurred in ICMP.exe

Additional information: Object reference not set to an instance of an object.

here is the class, if anyone can help I would greatly appreciate it.

Imports System.Net
Imports System.Net.Sockets

Class PingSomething
Const portICMP As Integer = 7
Const bufferHeaderSize As Integer = 8
Const packageHeaderSize As Integer = 28

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

Public Function Echo(ByVal RemoteName As String) As String
Dim RemoteHost As IPEndPoint 'address/port of remote host
Dim Identifier As Short = 0 'id of this packet
Dim Sequence As Short = 0 'sequence number of this packet
Dim DataSize As Byte = 32 'number of bytes of data to send
Dim ICMPSocket As Socket 'the socket we use to connect and send data through
Dim RequestBuffer() As Byte 'the request buffer
Dim ReplyBuffer(255) As Byte 'the reply buffer
Dim RecvSize As Integer = 0 'the number of bytes received
Dim Message As String

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)
Message = "Remote host " + RemoteHost.Address.ToString + " responded. " + (RecvSize - packageHeaderSize).ToString() + " bytes received."

Case Convert.ToByte(ICMPType.Unreachable)
Message = "Remote endpoint " + "unreachable."

Case Else
Message = "Received unexpected " + "data..."

End Select
End If

Catch e As Exception
Message = "Error: " + e.Message

Finally
If Not ICMPSocket Is Nothing Then
ICMPSocket.Close()
End If
End Try

Return Message
End Function

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


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