Results 1 to 13 of 13

Thread: Help with a class

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207

    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

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    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

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    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

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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...

  8. #8
    Addicted Member
    Join Date
    Apr 2002
    Location
    Anywhere but here
    Posts
    161
    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!

  9. #9
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Threading...
    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

  10. #10

  11. #11
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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:
    1. Private Function GetRemoteEndpoint(ByVal RemoteAddress As String) As IPEndPoint
    2.         Return New IPEndPoint(Dns.Resolve(RemoteAddress).AddressList(0), portICMP)
    3. End Function

    I wonder if there's a faster equivalent.

    -Lou

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:

  13. #13
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs down

    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
  •  



Click Here to Expand Forum to Full Width