Results 1 to 13 of 13

Thread: [RESOLVED] Hidden Characters after sending text via TCP

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    10

    Resolved [RESOLVED] Hidden Characters after sending text via TCP

    Hi, I'm new to the forums so please forgive me if I post this in the wrong section.
    Anyway I'm having a problem where if I convert a string to bytes then send it through TCP, on the other end it does not convert properly. The original string is present in the received bytes although there appears to be some hidden characters stopping me from using a select case. Rough code is below...

    TCP Message Server Code:
    1. Private Sub Listen()
    2.  
    3.         Dim LST_PORT as int16 = 9292    'Port to listen on'
    4.         Dim TCP_LISTEN As TcpListener = New TcpListener(LST_PORT)
    5.         TCP_LISTEN.Start()
    6.  
    7.         Try
    8.  
    9.             Dim TCP_CLIENT As TcpClient = TCP_LISTEN.AcceptTcpClient
    10.  
    11.             Dim NET_STREAM As NetworkStream = TCP_CLIENT.GetStream
    12.  
    13.             'reading data'
    14.             Dim bytes(TCP_CLIENT.ReceiveBufferSize) As Byte
    15.  
    16.             NET_STREAM.Read(bytes, 0, TCP_CLIENT.ReceiveBufferSize)
    17.  
    18.             Dim RX_STRING As String = Encoding.ASCII.GetString(bytes)
    19.  
    20.  
    21.             Select Case RX_STRING.Trim.ToUpper
    22.  
    23.                 Case "1"
    24.                     Console.Beep()
    25.                 Case "SHUTDOWN"
    26.                     Process.Start("shutdown", "-f -s -t 00")
    27.  
    28.                 Case Else
    29.  
    30.                     MsgBox(RX_STRING, MsgBoxStyle.OkOnly, "...")
    31.  
    32.  
    33.             End Select
    34.  
    35.             TCP_CLIENT.Close()
    36.  
    37.         Catch ex As Exception
    38.  
    39.         End Try
    40.         TCP_LISTEN.Stop()
    41.  
    42.  
    43.     End Sub


    Problem is if I send "beep" from the client program, rather than beeping the program instead follows the 'case else' and displays "beep" in a message box.
    Any help would be very much appreciated. Thank you.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hidden Characters after sending text via TCP

    Change
    Case "1"
    Console.Beep()


    to

    Case "beep"
    Console.Beep()
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    10

    Re: Hidden Characters after sending text via TCP

    Thanks for the reply, I should probably say that "1" used to say beep and that I just switched it to test. I have the same problem with sending "shutdown" so its not something as simple as that. Thanks for picking up on my mistake though

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Hidden Characters after sending text via TCP

    Quote Originally Posted by menathang
    Encoding.ASCII.GetString(bytes)
    ASCII - that's the problem, I think. Try Unicode.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hidden Characters after sending text via TCP

    It might be helpful to see what was sent. What is in bytes after
    NET_STREAM.Read(bytes, 0, TCP_CLIENT.ReceiveBufferSize)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    10

    Re: Hidden Characters after sending text via TCP

    Thanks cicatrix but that didn't seem to make a difference when I tried.
    dbasnett, on the client application I have used the same method, eg:
    Code:
    Dim SendBytes() as byte
    Dim SendString as string = "SHUTDOWN"
    
    SendBytes = Encoding.ASCII.getbytes()
    
    netstream.Write(SendBytes, 0, SendBytes.Length)
    I have also tried replacing "SHUTDOWN" with "BEEP" or in this case "1". It's just frustrating, the string starts of right but comes out somehow messed up. It's perfectly readable but won't go through a select case or if statement. And in the server which is a console application, it appears as if there are extra lines under the string that is received, I even tried trimming the string to no avail.

    As I said though, what is sent is "SHUTDOWN", what seems to come back is "SHUTDOWN" with a few extra blank lines.

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Hidden Characters after sending text via TCP

    What does Debug.Writeline(RX_STRING.Trim.ToUpper) show in the Sub Listen?

    Try to use StreamReader/StreamWriter instead of manual conversion to byte array

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hidden Characters after sending text via TCP

    Quote Originally Posted by dbasnett View Post
    It might be helpful to see what was sent. What is in bytes after
    NET_STREAM.Read(bytes, 0, TCP_CLIENT.ReceiveBufferSize)
    bytes(0)=
    bytes(1)=
    bytes(2)=
    etc.
    ?????????????????????
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    10

    Re: Hidden Characters after sending text via TCP

    Originally Posted by dbasnett
    It might be helpful to see what was sent. What is in bytes after
    NET_STREAM.Read(bytes, 0, TCP_CLIENT.ReceiveBufferSize
    Ok, I had a look and for the word "beep" I get the following for the first 6 elements,

    bytes(0)=98
    bytes(1)=101
    bytes(2)=101
    bytes(3)=112
    bytes(4)=0
    bytes(5)=0

    is the problem maybe something to do with the buffer size?, should there even be a 5th, 6th element, etc...

    I will also try using stream reader as cicatrix suggests if I can work that out, I use it for writing text files but that's about it usually. I'll take a look.

    Thank you for your help

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hidden Characters after sending text via TCP

    What does the send routine look like?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    10

    Re: Hidden Characters after sending text via TCP

    Send Routine Code:
    1. Private Sub SendP()
    2.  
    3.      
    4.  
    5.         Dim SendString As String = "beep"
    6.  
    7.         Dim tcpClient As New System.Net.Sockets.TcpClient
    8.         Try
    9.             tcpClient.Connect(HostAddress, Port)
    10.         Catch ex As Exception
    11.             MsgBox("Socket Err on port " & Port & "." & vbCrLf & ex.Message.ToString)
    12.             tcpClient.Close()
    13.             Console.ReadLine()
    14.  
    15.             Exit Sub
    16.  
    17.         End Try
    18.  
    19.  
    20.         Dim netstream As NetworkStream = tcpClient.GetStream
    21.  
    22.         If netstream.CanRead And netstream.CanWrite Then
    23.  
    24.             'Write'
    25.  
    26.             Dim sendbytes() As Byte = Encoding.ASCII.GetBytes(SendString)
    27.             MsgBox(sendbytes.Length)
    28.             tcpClient.SendBufferSize = sendbytes.Length
    29.  
    30.             netstream.Write(sendbytes, 0, sendbytes.Length)
    31.  
    32.  
    33.  
    34.         Else
    35.             MsgBox("Socket Error, port " & Port)
    36.             tcpClient.Close()
    37.  
    38.  
    39.         End If
    40.  
    41.         Console.Write("String Sent on port " & Port)
    42.  
    43.         tcpClient.Close()
    44.         Console.Clear()
    45.  
    46.        
    47.     End Sub

    Some extra info: The buffer size being sent is correct, eg the word beep has a buffer of length 4. On the other side I can't seem to retrieve the right size through recievebufferlength which always defaults at 8192 so the byte array on the server side is full of useless zeros...

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Hidden Characters after sending text via TCP

    So the answer might be to make all of the commands a fixed length:

    Code:
        Dim maxC As Integer = -1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim commands() As String = New String() {"shutdown", "beep", "hello"}
            For Each s As String In commands
                If s.Length > maxC Then maxC = s.Length
            Next
    
            Dim command2Send As String
            command2Send = foo(commands(0))
            command2Send = foo(commands(1))
            command2Send = foo(commands(2))
    
    
            Select Case command2Send.Trim
    
            End Select
        End Sub
    
        Private Function foo(ByVal s As String) As String
            foo = s.PadRight(maxC, " "c)
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    10

    Re: Hidden Characters after sending text via TCP

    Thanks, I think I might just do that as a nice way out. Starting a new computer science degree anyway so I might learn a trick when I do network programming

    Set length seems to work for now until I find a better way.

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