TCPListener and string formatting problems

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
 
C

Chad Z. Hower aka Kudzu

=?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/
 
J

Jon Skeet [C# MVP]

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top