CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2009
    Location
    U.S.A.
    Posts
    24

    Reading UDP Packets From UDP/IP

    I am writing a program that talks back and forth with an external machine through UDP/IP. I have figured out how to talk to the machine, bu now I need to know how to read the machine's response. I am using the following code:

    Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
    Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)


    TextBox1.Text = returnData

    When I break the code down and read receiveBytes, it comes through exactly the way I want it. It is falling apart when it gets encoded and put into a textbox. It just comes out as a "?". Does anybody know how I can do this? Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading UDP Packets From UDP/IP

    Post what you see on the wire, and what gets produced
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading UDP Packets From UDP/IP

    Likely the encoding again. use default encoding if you want the true byte by byte data.

  4. #4
    Join Date
    May 2009
    Location
    U.S.A.
    Posts
    24

    Re: Reading UDP Packets From UDP/IP

    I tried using Encoding.Default.GetBytes and instead of coming through as a "?" it came through as "—".

    When I put a break point in the code on:

    Dim returnData As String = Encoding.Default.GetString(receiveBytes)

    and I hover over

    Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)

    with my mouse it says:

    receiveBytes {Length = 8}

    Then I click on the plus symbol to get the drop down menu and it shows each byte in a drop down list:

    151 0 0 0 1 105 0 205

    These numbers in decimal are exactly what I want. Truly I want them displayed in HEX as:

    97 0 0 0 1 69 0 cd

    but I'll take what I can get. Then when I step to the next part of the code:

    Dim returnData as String = Encoding.Default.GetString(receiveBytes)

    and hover my mouse over "returnData" it shows up with "—" or when I use ASCII.GetBytes instead it comes up with "?".
    Last edited by bryan822; May 14th, 2009 at 07:48 AM.

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading UDP Packets From UDP/IP

    The -- may be due to the font you are using?

    Have you tried using the little string to hexstring routine that I posted in your other thread to convert the incoming string to a readable set of hex numbers?

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading UDP Packets From UDP/IP

    These are the lines from one of my projects.

    Send

    Code:
    sBytes = Encoding.Default.GetBytes(DataString)
    bSent = wSocket.Send(sBytes)

    Receive
    Code:
    bReceived = wSocket.Receive(rBytes)
    rDataString = System.Text.Encoding.Default.GetString(rBytes, 0, bReceived)

  7. #7
    Join Date
    May 2009
    Location
    U.S.A.
    Posts
    24

    Re: Reading UDP Packets From UDP/IP

    I ended up using your string to hex converter. I works out pretty good. I wasn't sure exactly how to incorporate it at first, but I ended up figuring it out. This is my code now:

    ' UdpClient.Receive blocks until a message is received from a remote host.
    Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
    Dim returnData As String = Encoding.Default.GetString(receiveBytes)

    udpClient.Close()

    Dim MyString As String = returnData
    Dim TmpString As String = ""
    Dim HexString As String = ""
    Dim OutputString As String = ""
    Dim X As Integer
    For X = 0 To MyString.Length - 1
    HexString = Asc(MyString.Substring(X, 1))

    TmpString = Hex(HexString).ToString.PadLeft(2, "0")
    OutputString = OutputString & TmpString
    Next
    TextBox1.Text = OutputString

    Once again, I appreciate your help. I'll tell you what. I would be really stuck without this forum.

  8. #8
    Join Date
    May 2009
    Location
    U.S.A.
    Posts
    24

    Re: Reading UDP Packets From UDP/IP

    Now the code is giving me a problem when I try to read multiple codes from the machine.




    'First String
    If FirstString = True Then
    Dim udpClient As New UdpClient("192.168.123.96", 50000)
    udpClient.Connect("192.168.123.96", 50000)

    Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(144) & Chr(0) & Chr(0) & Chr(0) & Chr(4) _
    & Chr(64) & Chr(0) & Chr(32))
    udpClient.Send(sendBytes, sendBytes.Length)
    Dim receiveBytes1 As [Byte]() = udpClient.Receive(RemoteIpEndPoint1)
    Dim returnData As String = Encoding.Default.GetString(receiveBytes1)
    Dim MyString As String = returnData
    Dim TmpString As String = ""
    Dim HexString As String = ""
    Dim OutputString As String = ""
    Dim X As Integer
    For X = 0 To MyString.Length - 1
    HexString = Asc(MyString.Substring(X, 1))
    TmpString = Hex(HexString).ToString.PadLeft(2, "0")
    OutputString = OutputString & TmpString
    OutputLength = OutputString.Length
    Next
    TextBox1.Text = OutputString
    FirstString = False
    If OutputString.StartsWith("910000000169004D") And OutputLength > 6 Then
    returnData = ""
    MyString = ""
    TmpString = ""
    HexString = ""
    OutputString = ""
    SecondString = True
    MotoTRBOtimer2 = DateTime.Now.Second
    End If
    udpClient.Close()
    End If

    'Second String
    MotoTRBOdelay2 = DateTime.Now.Second - MotoTRBOtimer2
    If MotoTRBOdelay2 > 2 Or (MotoTRBOdelay2 < 0 And MotoTRBOdelay2 > -57) Then
    If SecondString = True Then

    Dim udpClient As New UdpClient("192.168.123.96", 50000)
    udpClient.Connect("192.168.123.96", 50000)

    Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(150) & Chr(0) & Chr(0) & Chr(0) & Chr(4) _
    & Chr(64) & Chr(0) & Chr(32))
    udpClient.Send(sendBytes, sendBytes.Length)
    Dim receiveBytes2 As [Byte]() = udpClient.Receive(RemoteIpEndPoint2)
    Dim returnData As String = Encoding.Default.GetString(receiveBytes2)
    Dim MyString As String = returnData
    Dim TmpString As String = ""
    Dim HexString As String = ""
    Dim OutputString As String = ""
    Dim X As Integer
    For X = 0 To MyString.Length - 1
    HexString = Asc(MyString.Substring(X, 1))
    TmpString = Hex(HexString).ToString.PadLeft(2, "0")
    OutputString = OutputString & TmpString
    OutputLength = OutputString.Length
    Next
    TextBox1.Text = OutputString
    SecondString = False
    If OutputString = "970000000169004D" And OutputLength > 6 Then
    returnData = ""
    MyString = ""
    TmpString = ""
    HexString = ""
    OutputString = ""
    ThirdString = True
    MotoTRBOtimer3 = DateTime.Now.Second
    End If
    udpClient.Close()
    End If
    End If

    'Third String
    MotoTRBOdelay3 = DateTime.Now.Second - MotoTRBOtimer3
    If MotoTRBOdelay3 > 2 Or (MotoTRBOdelay3 < 0 And MotoTRBOdelay3 > -57) Then
    If ThirdString = True Then

    Dim udpClient As New UdpClient("192.168.123.96", 50000)
    udpClient.Connect("192.168.123.96", 50000)

    Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(112) & Chr(0) & Chr(0) & Chr(0) _
    & Chr(4) & Chr(0) & Chr(12) & Chr(0) & Chr(3) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) _
    & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0))
    udpClient.Send(sendBytes, sendBytes.Length)
    Dim receiveBytes3 As [Byte]() = udpClient.Receive(RemoteIpEndPoint3)
    Dim returnData As String = Encoding.Default.GetString(receiveBytes3)
    Dim MyString As String = returnData
    Dim TmpString As String = ""
    Dim HexString As String = ""
    Dim OutputString As String = ""
    Dim X As Integer
    For X = 0 To MyString.Length - 1
    HexString = Asc(MyString.Substring(X, 1))
    TmpString = Hex(HexString).ToString.PadLeft(2, "0")
    OutputString = OutputString & TmpString
    OutputLength = OutputString.Length
    Next
    TextBox1.Text = OutputString
    ThirdString = False
    If OutputString = "7000000001001300020000000000060000000700000001010101" And OutputLength > 6 Then
    returnData = ""
    MyString = ""
    TmpString = ""
    HexString = ""
    OutputString = ""
    FourthString = True
    MotoTRBOtimer4 = DateTime.Now.Second
    End If
    udpClient.Close()
    End If
    End If




    The First String and the Second String work fine, but for some reason when it gets to the Third String it locks up when I try to declare the value "receiveBytes3". It will all of a sudden just stop and freeze up. If I put a break point on this part of the code and step through it, it will work though. I tried using timers to slow it down. I also tried using "System.Threading.Thread.Sleep" to slow it down thinking maybe something in the code just needed a second to clear itself out. Neither worked. Does anybody have any ideas? Thank you.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading UDP Packets From UDP/IP

    Use code tags and indent your code. To hard to read as is.

    like this but with an e

    [cod][/cod]

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading UDP Packets From UDP/IP

    I haven't worked with UDP in dot net at all and have not used UDP in years so I am not sure how much it works like TCP but I would think many of the methods are the same.

    A couple of things I noticed are there seems to be a lot more code there than is needed but that is not a big deal at this point. More importantly you are sending then reading data. You really should be checking the port status for errors and then looking to see if there is anything to read before you issue a read statement. The method you are using could very well cause the program to hang under certian conditions. I also do not understand why you are opening and closing the connection for every string you send other than it looks like you just copied the code and repeated the entire piece 3 times.

    Here is an edited section of code from one of my projects which uses TCP.
    Code:
    bTimeOut = TimeOfDay
                    Do While wSocket.Poll(1, SelectMode.SelectRead) = False
                        bTimeTaken = DateDiff(DateInterval.Second, bTimeOut, TimeOfDay)
                        If bTimeTaken > wTimeout Then
                            Disconnect()
                            ErrFlag = True
                            Return "ERR:6:Receive Timeout"
                        End If
                        Application.DoEvents()
                    Loop
                    Try
                        If wSocket.Available < 1 Then
                            ErrFlag = True
                            Disconnect()
                            Return "ERR:7:No Data available. Connection may be broken."
                        End If
                        bReceived = wSocket.Receive(rBytes)
                        rDataString = System.Text.Encoding.Default.GetString(rBytes, 0, bReceived)
                        mByte = Right(rDataString, 1)
                        Do While mByte <> Chr(4)
                            Do While wSocket.Available > 0
                                bReceived = wSocket.Receive(rBytes)
                                rDataString = rDataString & System.Text.Encoding.ASCII.GetString(rBytes, 0, bReceived)
                                Application.DoEvents()
                            Loop
                            mByte = Right(rDataString, 1)
                        Loop
                        Return Mid(rDataString, 1, Len(rDataString) - 1) 
                    Catch ex As Exception
                        ErrFlag = True
                       Disconnect()
                        Return "ERR:8:" & ex.Message
                    End Try
    In my case the data will always be terminated by the character 4 so the code will loop until it sees that as the last character to make sure it received the entire string from the host. My receive buffer in this case is defined as 1k but sometimes the data received is larger.
    Last edited by DataMiser; May 18th, 2009 at 09:05 AM.

  11. #11
    Join Date
    May 2009
    Location
    U.S.A.
    Posts
    24

    Re: Reading UDP Packets From UDP/IP

    I just found out that it's not actually giving me the problem when it reads it. I'm still not able to read from the UDP/IP, but something different is going on when I try to send to the machine a third time. I am using Wireshark to monitor the UDP/IP activity. When I send the first two parts of the code:

    Code:
    'First String
            If FirstString = True Then
    
                Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(144) & Chr(0) & Chr(0) & Chr(0) & Chr(4) _
                    & Chr(64) & Chr(0) & Chr(32))
                UdpClient.Send(sendBytes, sendBytes.Length)
                Dim receiveBytes1 As [Byte]() = UdpClient.Receive(RemoteIpEndPoint1)
                Dim returnData As String = Encoding.Default.GetString(receiveBytes1)
                Dim MyString As String = returnData
                Dim TmpString As String = ""
                Dim HexString As String = ""
                Dim OutputString As String = ""
                Dim X As Integer
                For X = 0 To MyString.Length - 1
                    HexString = Asc(MyString.Substring(X, 1))
                    TmpString = Hex(HexString).ToString.PadLeft(2, "0")
                    OutputString = OutputString & TmpString
                    OutputLength = OutputString.Length
                Next
    
                TextBox1.Text = OutputString
                FirstString = False
                'If OutputString.StartsWith("910000000169004D") And OutputLength >= 20 Then
                If OutputLength >= 20 Then
                    returnData = ""
                    MyString = ""
                    TmpString = ""
                    HexString = ""
                    OutputString = ""
                    SecondString = True
                    MotoTRBOtimer2 = DateTime.Now.Second
                End If
            End If
    
            'Second String
            MotoTRBOdelay2 = DateTime.Now.Second - MotoTRBOtimer2
            If MotoTRBOdelay2 > 2 Or (MotoTRBOdelay2 < 0 And MotoTRBOdelay2 >= -57) Then
                If SecondString = True Then
    
                    Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(150) & Chr(0) & Chr(0) & Chr(0) & Chr(4) _
                        & Chr(64) & Chr(0) & Chr(32))
                    udpClient.Send(sendBytes, sendBytes.Length)
                    Dim receiveBytes2 As [Byte]() = udpClient.Receive(RemoteIpEndPoint2)
                    Dim returnData As String = Encoding.Default.GetString(receiveBytes2)
                    Dim MyString As String = returnData
                    Dim TmpString As String = ""
                    Dim HexString As String = ""
                    Dim OutputString As String = ""
                    Dim X As Integer
                    For X = 0 To MyString.Length - 1
                        HexString = Asc(MyString.Substring(X, 1))
                        TmpString = Hex(HexString).ToString.PadLeft(2, "0")
                        OutputString = OutputString & TmpString
                        OutputLength = OutputString.Length
                    Next
    
                    TextBox1.Text = OutputString
                    SecondString = False
                    'If OutputString = "970000000169004D" And OutputLength > 6 Then
                    If OutputLength >= 16 Then
                        returnData = ""
                        MyString = ""
                        TmpString = ""
                        HexString = ""
                        OutputString = ""
                        ThirdString = True
                        MotoTRBOtimer3 = DateTime.Now.Second
                    End If
                End If
            End If
    it says "Source port: cppdp Destination port: 50000" when it sends and
    "Source port 50000 Destination port: cppdp" when it receives.

    On the third section, where I am having the problem:

    Code:
    'Third String
            MotoTRBOdelay3 = DateTime.Now.Second - MotoTRBOtimer3
            If MotoTRBOdelay3 > 1 Or (MotoTRBOdelay3 < 0 And MotoTRBOdelay3 >= -59) Then
                If ThirdString = True Then
    
                    'Send
                    Dim sendBytes As [Byte]() = Encoding.Default.GetBytes(Chr(112) & Chr(0) & Chr(0) & Chr(0) _
                & Chr(4) & Chr(0) & Chr(12) & Chr(0) & Chr(3) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) _
                & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0))
                    udpClient.Send(sendBytes, sendBytes.Length)
    
                    'Recieve
                    Dim receiveBytes3 As [Byte]() = udpClient.Receive(RemoteIpEndPoint3)
                    Dim returnData As String = Encoding.Default.GetString(receiveBytes3)
                    Dim MyString As String = returnData
                    Dim TmpString As String = ""
                    Dim HexString As String = ""
                    Dim OutputString As String = ""
                    Dim X As Integer
                    For X = 0 To MyString.Length - 1
                        HexString = Asc(MyString.Substring(X, 1))
                        TmpString = Hex(HexString).ToString.PadLeft(2, "0")
                        OutputString = OutputString & TmpString
                        OutputLength = OutputString.Length
                    Next
    
                    TextBox1.Text = OutputString
                    ThirdString = False
                    'If OutputString = "7000000001001300020000000000060000000700000001010101" And OutputLength > 6 Then
                    If OutputLength >= 52 Then
                        returnData = ""
                        MyString = ""
                        TmpString = ""
                        HexString = ""
                        OutputString = ""
                        FourthString = True
                        MotoTRBOtimer4 = DateTime.Now.Second
                    End If
                End If
            End If
    it says "Destination unreachable (Port unreachable)"

    So now I'm wondering if there is maybe a way, or even a need, to discard any outgoing information after sending each time.

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading UDP Packets From UDP/IP

    You really should have some tests in there to insure that the port is ready to send or receive data and check the port error status as well before you attempt to send or receive anything. Failure to do so can cause many odd problems.

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

Featured