|
-
Oct 1st, 2002, 05:43 PM
#1
Thread Starter
Addicted Member
Help with a class
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
-
Oct 1st, 2002, 07:04 PM
#2
Fanatic Member
Dunno, I cut and pasted your source code and TextBox2.Text came back as:
Remote host (my IP address) responded. 32 bytes received.
i.e. it worked for me. Maybe you need a service pack or something?
Running Windows 2000, everything at latest service pack.
-
Oct 1st, 2002, 08:49 PM
#3
Thread Starter
Addicted Member
I do have al the latest service packs installed but I also have everett installed in addition to visual studios.net.
Maybee I will end up removing it, there are somethings I don't like about it anyways.
Jeremy
-
Oct 1st, 2002, 08:58 PM
#4
Thread Starter
Addicted Member
Nevermind, finally got it to work in everett but still no luck in the regular .net, oh well, I guess I will just have to import the project and work on it in the new version.
Jeremy
-
Oct 1st, 2002, 08:59 PM
#5
PowerPoster
What don't you like about Everett? I have been out of town on business the last month so I haven't been able to install it yet.
-
Oct 1st, 2002, 09:25 PM
#6
Thread Starter
Addicted Member
My biggest gripe, and it will be a suggestion to remove, is that put puts a solid line across the screen right below the end of a function, and module, kind of like vb6 had the line, would bother me if I could shut it of but I don't see a spot. Other than that it just has a few quirks.
Jeremy
-
Oct 2nd, 2002, 11:15 PM
#7
PowerPoster
Hmmm, doesn't seem like too big of a deal for me, I kind of like it because I scroll too fast to always see where one method ends and the other begins. But your right, there should be a way to turn it off. Did you try it with the C# developer profile? Maybe it doesn't include it there...
-
Oct 17th, 2003, 09:43 AM
#8
Addicted Member
I know... I know... Its 1 year later and I have a question on this code. I’ve been looking high and low for code to do some pinging with .net and this seems to be the best so far. My only gripe is its slow as all get out. Pings take 10-15 seconds each to respond. In VB6 I can ping 20 PC's in that time. Any Ideas on how to speed this up?
Thanks
-------------------------
My name says it all!
-
Oct 18th, 2003, 12:03 AM
#9
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Apr 13th, 2004, 06:25 PM
#10
-
May 9th, 2004, 04:34 PM
#11
Originally posted by CoderNewbie
I know... I know... Its 1 year later and I have a question on this code. I’ve been looking high and low for code to do some pinging with .net and this seems to be the best so far. My only gripe is its slow as all get out. Pings take 10-15 seconds each to respond. In VB6 I can ping 20 PC's in that time. Any Ideas on how to speed this up?
Thanks
It seems that the biggest time consumer is the function:
VB Code:
Private Function GetRemoteEndpoint(ByVal RemoteAddress As String) As IPEndPoint
Return New IPEndPoint(Dns.Resolve(RemoteAddress).AddressList(0), portICMP)
End Function
I wonder if there's a faster equivalent.
-Lou
-
May 9th, 2004, 04:58 PM
#12
I think you'd have to write your own DNS resultion method to speed that up. As for the original post (although its old) you can turn off the lines between the procedures from the options:
-
May 13th, 2004, 01:10 PM
#13
Frenzied Member
I've tried the code in VS.NET 2003 and it doesn't work.
Error: A non-blocking socket operation could not be completed immediately.
~Peter

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|