TCP returns weird characters

This is my code:
bool tcpQuery(std::string query, std::string& rtn) {
int recv_size = 0, attempts = 0;
char rsp[MAX_REPLY_LEN] = { NULL };

D(std::cout << "TCP query: " << query << std::endl;);

/*tcpCommand(query);*/

if ((recv_size = recv(tcpsocket, rsp, MAX_REPLY_LEN, 0)) == SOCKET_ERROR) {
while ((recv_size = recv(tcpsocket, rsp, MAX_REPLY_LEN, 0)) == SOCKET_ERROR) {
Sleep(1);
if (attempts++ > 5) {
D(std::cout << "Error: " << WSAGetLastError() << "\n";);
return false;
}
}
}


std::cout << "Query Size: " << recv_size << std::endl;

rsp[recv_size] = NULL;



rtn = std::string(rsp);
std::cout << "The lenth of RTN is : " << rtn.length() << std::endl;
D(std::cout << "TCP query return: " << rtn << std::endl;);
Sleep(TCP_DELAY);

return true;
}

The program returns that I have successfully connected and says that I have a return length of 3 but then gives me weird characters for what is in the returned query see below.

Initializing Winsock with IPAddress: 192.168.1.2
status: 1
TCP query: acc
Query Size: 3
The legnth of RTN is : 3
TCP query return:  √

there is also a square with t a ? in it next to the check mark.

Can anyone tell me what I am doing wrong?
your sample doesn't compile and is lacking formatting.

1
2
rsp[recv_size] = NULL;
rtn = std::string(rsp);


constructing string from nullptr? that doesn't seem like normal.
What is the value of MAX_REPLY_LEN?

constructing string from nullptr? that doesn't seem like normal.
The data in rsp is not necessarily null-terminated after a call to recv, so he's adding his own, it looks like.
Not that I'm saying it's correct or not, I can't tell.
Last edited on
ok, that is gonna make peoples heads explode lol.
null and nullptr are named constants for zero, sure, but please do not use them for zero or in place of other constants.
'\0' is the null terminal for c-strings, if you want to type 4 chars where 1 will do.
I just use 0 directly. Its less visual clutter and anyone using c-strings will understand it. If that bugs you, make your own constant or use the literal clutter value. But please, do not use pointer constant for zero terminal string. It will work, but it will have all your peers looking for your head with an axe.

back on topic, not sure what exactly you are up to but I will tell you that most databases allow you to use non ascii encodings and if you try to read that as ascii you will get weirdness. So even if you do everything right you can have this effect, so if you believe you are doing things right check the settings before trying to debug a non-bug. Nothing is worse than assuming your code is wrong when its doing what you wanted and just got scrambled input -- you will invariably 'fix' the correct code, breaking it.. It could also be that you have asked for something stored in binary and jacked it into text. Have you done a raw hex comparison of expected value vs value you got back?
Last edited on
TCP returns weird characters
Garbage in, garbage out. What did the sender send? Are you certain that it sent ASCII data? Do you have the stream open in a mode that will strip out the packet headers?
> rsp[recv_size] = NULL;
This is a buffer overflow if recv() actually fills the buffer with MAX_REPLY_LEN chars.

It's also unnecessary, since the string ctor will accept a length anyway.
rtn = std::string(rsp,recv_size);
Topic archived. No new replies allowed.