TCPListener and string formatting problems

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I need a TCPListener and a TCPClient to communicate correctly
TCPListener sends this

string strData = "ABC"
byte[] data = Encoding.ASCII.GetBytes(strData)
s.Send(data, data.Length,0)

TCPClient receives
Byte[] read = new Byte[32]
String Data = Encoding.ASCII.GetString(read)

When I print the result in a textBox/messageBox it look like this: ABC
but if I try to test i
if(Data=="ABC"


it never works

I believe it has to do with the string -> charArray -> byte[] -> string formatting, but can't figure it out..
Please help
Thanks
 
=?Utf-8?B?VGhvbWFz?= said:
I believe it has to do with the string -> charArray -> byte[] -> string
formatting, but can't figure it out... Please help!

If you are open to using open source comonents, there is a TCP client /
server demo here:
www.atozed.com/indy

which demonstrates what you are trying to do.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
Thomas said:
I need a TCPListener and a TCPClient to communicate correctly.
TCPListener sends this:

string strData = "ABC";
byte[] data = Encoding.ASCII.GetBytes(strData);
s.Send(data, data.Length,0);

TCPClient receives:
Byte[] read = new Byte[32];
String Data = Encoding.ASCII.GetString(read);

When I print the result in a textBox/messageBox it look like this: ABC,
but if I try to test it
if(Data=="ABC")
{
}
it never works.

Well, you haven't really shown how you've read the data - you've shown
creating anew byte array, but not how the data gets into it. The
problem may well be that your byte array *is* 32 bytes long, even
though you've only read three bytes of data. You're then decoding all
32 bytes, so you've got a string with ABC and then 29 null characters -
which isn't the same as the string "ABC".
 
Back
Top