Computing.Net > Forums > Programming > VB.NET TCP Server

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB.NET TCP Server

Reply to Message Icon

Name: wille
Date: December 18, 2008 at 06:31:48 Pacific
OS: WINDOWS XP Home edition S
CPU/Ram: 3.2GHz/1024Mb
Product: Unknown / UNKNOWN
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: December 20, 2008 at 19:22:25 Pacific
Reply:

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))


0

Response Number 2
Name: wille
Date: December 22, 2008 at 16:30:19 Pacific
Reply:

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 Function

However, 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


0

Response Number 3
Name: wille
Date: December 22, 2008 at 16:44:59 Pacific
Reply:

Never mind, I solved that too. Anyway, Thanks for your help Razor.

Live the life as you know it
/ Wille


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VB.NET TCP Server

VB.NET Programming for SQL Server www.computing.net/answers/programming/vbnet-programming-for-sql-server/8306.html

need help in vb.net www.computing.net/answers/programming/need-help-in-vbnet/5974.html

VB.NET Pocket PC Application www.computing.net/answers/programming/vbnet-pocket-pc-application/9827.html