|
-
Jan 19th, 2010, 02:56 AM
#1
Thread Starter
New Member
[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.
-
Jan 19th, 2010, 09:48 AM
#2
Re: Hidden Characters after sending text via TCP
Change
Case "1"
Console.Beep()
to
Case "beep"
Console.Beep()
-
Jan 19th, 2010, 10:56 PM
#3
Thread Starter
New Member
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
-
Jan 20th, 2010, 08:38 AM
#4
Re: Hidden Characters after sending text via TCP
 Originally Posted by menathang
Encoding.ASCII.GetString(bytes)
ASCII - that's the problem, I think. Try Unicode.
-
Jan 20th, 2010, 09:31 AM
#5
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)
-
Jan 21st, 2010, 05:29 AM
#6
Thread Starter
New Member
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.
-
Jan 21st, 2010, 06:47 AM
#7
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
-
Jan 21st, 2010, 08:18 AM
#8
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)
bytes(0)=
bytes(1)=
bytes(2)=
etc.
?????????????????????
-
Jan 24th, 2010, 12:12 AM
#9
Thread Starter
New Member
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
-
Jan 24th, 2010, 09:12 AM
#10
Re: Hidden Characters after sending text via TCP
What does the send routine look like?
-
Jan 24th, 2010, 09:44 AM
#11
Thread Starter
New Member
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...
-
Jan 24th, 2010, 12:57 PM
#12
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
-
Jan 27th, 2010, 04:44 AM
#13
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|