[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:
Private Sub Listen()
Dim LST_PORT as int16 = 9292 'Port to listen on'
Dim TCP_LISTEN As TcpListener = New TcpListener(LST_PORT)
TCP_LISTEN.Start()
Try
Dim TCP_CLIENT As TcpClient = TCP_LISTEN.AcceptTcpClient
Dim NET_STREAM As NetworkStream = TCP_CLIENT.GetStream
'reading data'
Dim bytes(TCP_CLIENT.ReceiveBufferSize) As Byte
NET_STREAM.Read(bytes, 0, TCP_CLIENT.ReceiveBufferSize)
Dim RX_STRING As String = Encoding.ASCII.GetString(bytes)
Select Case RX_STRING.Trim.ToUpper
Case "1"
Console.Beep()
Case "SHUTDOWN"
Process.Start("shutdown", "-f -s -t 00")
Case Else
MsgBox(RX_STRING, MsgBoxStyle.OkOnly, "...")
End Select
TCP_CLIENT.Close()
Catch ex As Exception
End Try
TCP_LISTEN.Stop()
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.:)
Re: Hidden Characters after sending text via TCP
Change
Case "1"
Console.Beep()
to
Case "beep"
Console.Beep()
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
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.
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)
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.
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
Re: Hidden Characters after sending text via TCP
Quote:
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)
bytes(0)=
bytes(1)=
bytes(2)=
etc.
?????????????????????
Re: Hidden Characters after sending text via TCP
Quote:
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
Re: Hidden Characters after sending text via TCP
What does the send routine look like?
Re: Hidden Characters after sending text via TCP
Send Routine Code:
Private Sub SendP()
Dim SendString As String = "beep"
Dim tcpClient As New System.Net.Sockets.TcpClient
Try
tcpClient.Connect(HostAddress, Port)
Catch ex As Exception
MsgBox("Socket Err on port " & Port & "." & vbCrLf & ex.Message.ToString)
tcpClient.Close()
Console.ReadLine()
Exit Sub
End Try
Dim netstream As NetworkStream = tcpClient.GetStream
If netstream.CanRead And netstream.CanWrite Then
'Write'
Dim sendbytes() As Byte = Encoding.ASCII.GetBytes(SendString)
MsgBox(sendbytes.Length)
tcpClient.SendBufferSize = sendbytes.Length
netstream.Write(sendbytes, 0, sendbytes.Length)
Else
MsgBox("Socket Error, port " & Port)
tcpClient.Close()
End If
Console.Write("String Sent on port " & Port)
tcpClient.Close()
Console.Clear()
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...
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
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.