Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetBytes not working for Sockets

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I am trying to Send data to a socket using GetBytes. The problem is that GetBytes when using a string parameter sends exactly the amount of characters in the string without a null byte. So the listener gets only the amount of data sent - that is if you do this:

UTF8Encoding enc = new UTF8Encoding();
output = "testing";
sock.Send(enc.GetBytes(output),7,0);
This will send 7 bytes and no null byte.

If you do:

UTF8Encoding enc = new UTF8Encoding();
output = "testing";
sock.Send(enc.GetBytes(output));

It will send 7 bytes but the recieving size thinks you send an unknown number of bytes (3349211923). This will overflow the buffer. But this is way all the examples on the net say to do it.

How do you get around this?

Thanks,

Tom
 
Actually, I figured out the problem.

It was passing the correct number of character - 7. But not the null byte so that when I did an strcpy, there was no null byte to stop it so it went until it found one which caused a pointer to be trashed.

I found I have to add the null byte myself at the end of the string in a byte array.

Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top