Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi, I've got a TCP server and client which uses a NetworkStream and communicates good. There's a problem though; On each side I can recieve and display data but the program can not read it as a normal string. For example, if the server recieves "exit" from the client it'l use
Dim bytes(tcpc.ReceiveBufferSize) As Byte
Dim clientdata As String
NetStream.Read(bytes, 0, CInt(tcpc.ReceiveBufferSize))
clientdata = System.Text.Encoding.ASCII.GetString(bytes)this works fine to just output through a messagebox or the console to show the user what's recieved, but it can't use
if clientdata = "exit" then me.close ' or something like that
clientdata and "exit" will allways be separate no matter what i try, like clientdata.tostring or clientdata.tostring.tolower
what's wrong?
thanks in advance
Live the life as you know it
/ Wille
The problem is the unused portion of your buffer; Nulls are valid elements in your String, but may not be displayed. Try this:
clientdata = System.Text.Encoding.ASCII.GetString(bytes)
clientdata = clientdata.Substring(0, clientdata.IndexOf(0))
Report Offensive Follow Up For Removal
No that didn't work but you were right, the NULLs in the string were causing the problem, I solved it with this.
Public Function NullCheck(ByVal data As String)
Dim rstr As String = ""
For Each cch As Char In data
If not cch = Nothing Then
rstr = rstr & cch
End If
Next
Return rstr
End FunctionHowever, while this worked, another problem turned up. If I send a string and recieve it, it can be read and displayed. But after that, if I send a shorter string, the difference will be displayed in the characters sent by the before string; Like this, if I first send "this works!" and then "not now", the second will become "not nowks". It'll take the "ks" from the first one. Why is that? Thanks for your help Razor2.3, I really appreciate it.
Live the life as you know it
/ Wille
Report Offensive Follow Up For Removal
Never mind, I solved that too. Anyway, Thanks for your help Razor.
Live the life as you know it
/ Wille
Report Offensive Follow Up For Removal
![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |