Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

.NET CF - Received data is corrupt?

Back to Forum: Tech Off
  • User profile image
    ZippyV

    I made a little client-server application.
    The server-application holds a mediaplayer control and the client application (on a pda, connected using infrared) sends the commands "play" or "stop" to control the mediaplayer.
    The problem is with the string I receive. The stream puts the received data in a buffer. The buffer is an array of 100 bytes and the first four bytes are characters (play or stop). The buffer is converted to a string and this is where something weird happens: when I hover over my code, the tooltip says:

    sCommand = "play

    Notice there is no " at the end of play and that means that PlaySong() or StopSong() will never be executed:

    Select Case sCommand
       Case "play"
          PlaySong()
       Case "stop"
          StopSong()
    End Select

    Why does this happen?

  • User profile image
    Secret​Software

    ZippyV wrote:
    I made a little client-server application.
    The server-application holds a mediaplayer control and the client application (on a pda, connected using infrared) sends the commands "play" or "stop" to control the mediaplayer.
    The problem is with the string I receive. The stream puts the received data in a buffer. The buffer is an array of 100 bytes and the first four bytes are characters (play or stop). The buffer is converted to a string and this is where something weird happens: when I hover over my code, the tooltip says:

    sCommand = "play

    Notice there is no " at the end of play and that means that PlaySong() or StopSong() will never be executed:

    Select Case sCommand
       Case "play"
          PlaySong()
       Case "stop"
          StopSong()
    End Select

    Why does this happen?


    How do you extract the command string from the stream? maybe your not taking the exact bytes from the stream. (ie, not including the " byte)?

    Show code for extraction of the sCommand from buffer.

  • User profile image
    Randolpho

    I agree with SecretSoftware -- the problem is in your extraction code somewhere.

     

    Also, please stop perpetuating the evil that is pseudo-hungarian. kthxbye

  • User profile image
    ZippyV

        Private Sub CheckForData()
            Dim bBuffer(100) As Byte
            Dim sCommando As String

            lListener = New System.Net.Sockets.IrDAListener("mediaserver")

            Try
                lListener.Start()
                iClient = lListener.AcceptIrDAClient()

                Do While iClient.GetStream().Read(bBuffer, 0, 100) > 1
                    sCommando = System.Text.Encoding.ASCII.GetString(bBuffer)
                    Debug.WriteLine(sCommando)
                    Select Case sCommando
                        Case "play"
                            PlaySong()
                        Case "stop"
                            StopSong()
                    End Select
                    sCommando = String.Empty
                Loop

            Catch exSocket As System.Net.Sockets.SocketException
            Catch exIO As System.IO.IOException

            Finally
                lListener.Stop()
                iClient.Close()
            End Try
        End Sub

  • User profile image
    Maddus Mattus

    Maybe just trim the string?

  • User profile image
    Sven Groot

    You are asking GetString to translate a 101 bytes long array to a string, and that's what it does. It will include all those 101 bytes in the string, which in this case includes a lot of zero characters (remember that .Net strings are not zero-terminated unlike C strings, so they can include zero characters). The debugger can't handle that and does think the string ends at the zero character, hence the missing ".

    Store the return value from Read in a variable (called e.g. bytesRead), and then use Encoding.ASCII.GetString(bBuffer, 0, bytesRead).

    Since you're reusing the buffer this would've also given you strange results if the second string received is shorter than the first one, since the characters from the first Read call would still be in the buffer.

  • User profile image
    Secret​Software

    ZippyV wrote:
    

        Private Sub CheckForData()
            Dim bBuffer(100) As Byte
            Dim sCommando As String

            lListener = New System.Net.Sockets.IrDAListener("mediaserver")

            Try
                lListener.Start()
                iClient = lListener.AcceptIrDAClient()

                Do While iClient.GetStream().Read(bBuffer, 0, 100) > 1
                    sCommando = System.Text.Encoding.ASCII.GetString(bBuffer)
                    Debug.WriteLine(sCommando)
                    Select Case sCommando
                        Case "play"
                            PlaySong()
                        Case "stop"
                            StopSong()
                    End Select
                    sCommando = String.Empty
                Loop

            Catch exSocket As System.Net.Sockets.SocketException
            Catch exIO As System.IO.IOException

            Finally
                lListener.Stop()
                iClient.Close()
            End Try
        End Sub



    Private Sub CheckForData()
     Dim bBuffer(100) As Byte
     Dim sCommando As String
     lListener = New System.Net.Sockets.IrDAListener("mediaserver")
     Try
       lListener.Start
       iClient = lListener.AcceptIrDAClient
       Dim bytesRead As Integer = 0
       While ((bytesRead = (iClient.GetStream.Read(bBuffer, 0, 100)) > 1)
         sCommando = System.Text.Encoding.ASCII.GetString(bBuffer, 0, bytesRead)
         Debug.WriteLine(sCommando)
         If sCommando = "play" Then
           PlaySong
         Else
           If sCommando = "stop" Then
             StopSong
           End If
         End If
         sCommando = String.Empty
         Array.Clear(bBuffer, 0, bBuffer.Length)
         bytesRead = 0
       End While
     Catch exSocket As System.Net.Sockets.SocketException
     Catch exIO As System.IO.IOException
     Finally
       lListener.Stop
       iClient.Close
     End Try
    End Sub

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.