socket recv

Apr 22, 2012 at 11:07pm
im having a problem while receiving a message from a client to server. i always get extra characters. let me show that:

[Client]
1
2
3
{...
send(Connect, "my msg", 6 ,NULL);
...}


[Server]
1
2
3
4
5
{...
recv(Connect, charmsg, sizeof(charmsg), NULL);
strmsg = charmsg;
cout << strmsg << endl;
...}


the message i receive is: my msg╠╠╠╠U╗ x@²▬


my question is how do i get rid of those extra chars?
Apr 22, 2012 at 11:11pm
They're not part of the message, it's whatever was in the buffer prior to the call to recv.
recv returns how many bytes were read into the buffer when it is successful.
Apr 23, 2012 at 1:15am
closed account (zvRX92yv)
I'm not good on socket programming but.. Aren't you supposed to include the null terminator?
std::cout needs a null terminator..
Last edited on Apr 23, 2012 at 1:18am
Apr 23, 2012 at 1:32am
To clarify, both of the above are correct. "my msg" is actually 7 chars, including a null terminator for the C-string it defines. You only send 6, so when you print it via cout, it doesn't hit a null character so it continues off until it does. Either print exactly the number of chars you recv'd using Athar's suggestion, or send the null terminator.
Apr 23, 2012 at 12:44pm
Thanks for the help, it works
Topic archived. No new replies allowed.