RESOLVED. char issue, strange characters
----RESOLVED-----
Hi All,
This should be an easy one for you.
I have defined and set a char as follows:
char *data = "1";
The information is sent over a SOCKET as such:
send(testconnection,data,strlen(data),0);
And when received I essentially have to guess as to what the content it will hold is (because it may not always be "1, it may be "blahblah" etc), so when I receive it I am using:
char data[512];
if(recv(client, data, sizeof(data),0)) {
//data[sizeof(data)] = "\0";]
cout << "sizeof:" << sizeof(data) << "\n";
cout << "Menu option:" << data << "\n";
}
Now, as I am sure you will know, my cout << "Menu option:" << data << "\n"; returns my character 1. followed by a considerable amount of filler characters (to meet the full 512) as shown below:
Menu option:1╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠►
So, how would I go about fixing this so that it shows the value nicely??
Re: char issue, strange characters
The problem has been resolved. I added: char data[512] = {'/0'}; at the receiving end.
Re: char issue, strange characters
That's not *really* a resolution. What you want to do is send one additional byte on the sending end, so that the terminating NULL goes across the wire.
Re: char issue, strange characters
Quote:
Originally Posted by
Lindley
That's not *really* a resolution. What you want to do is send one additional byte on the sending end, so that the terminating NULL goes across the wire.
I am not sure whether this is a good solution. Network applications are supposed to be independent of OS, language, etc. Just because C/C++ uses NULL terminated strings, does not mean that we should modify the network protocol to send extra NULL in the end.
I think the problem is printing of the data and not sending of the data. So, while printing, NULL should be added, if one wants to treat it as a NULL terminated string literal.
Re: char issue, strange characters
Quote:
Originally Posted by
rohshall
I am not sure whether this is a good solution. Network applications are supposed to be independent of OS, language, etc. Just because C/C++ uses NULL terminated strings, does not mean that we should modify the network protocol to send extra NULL in the end.
I think the problem is printing of the data and not sending of the data. So, while printing, NULL should be added, if one wants to treat it as a NULL terminated string literal.
Great, so how do you propose to solve the printing problem? This interface on the receive end doesn't have any other way of knowing how much data to expect so there is no way to know where to insert the NULL character. Lindley's idea is the only reasonable solution that has been proposed. The only other way I can think of using this particular mechanism is to pack the number of bytes sent into the first character. Then you'll have to parse the buffer after being copied which would be fairly easy. Better yet, use a structure and define a message with a header and a body so that the receiver can intelligently "unpack" the message.
Re: char issue, strange characters
Quote:
Originally Posted by
kempofighter
The only other way I can think of using this particular mechanism is to pack the number of bytes sent into the first character.
This is how most of the network protocols do it, I think.