Reprint: http://blog.csdn.net/highfly591/article/details/45309239
Regardless of the data type in the process, the data that is transmitted to the sending interface must be char *, and the length of the string is explicitly counted in the best process.
Packetlen= (16+cmdinfo.length ())) *sizeof (char);//For simple string or qstring can use the C + + to take the length of the function to take the length of the string, If the structure of C is cast to char*, such as the concatenation of the string, do not use the custom function to take the length, must be divided in the process of summing.
memcpy (Cmdpacket, (char *) &dataheadinfo,16);
memcpy (cmdpacket+16, (char *) (CMDINFO.C_STR ()), cmdinfo.length ());
Especially in the process language. such as QT, the process may use qstring,std::string, to the end will be converted to char *
For the transport structure type of the protocol header, must not be the structure, first converted to a string, and then on the connection of the package body, so that after the transmission of the past, the length of the Baotou is not the fixed length of your structure, when transferred to a string, the string is based on the length of your actual occupied, allocating memory. Must be less than the length of your fixed structure.
No
Cheadinfo is a C structural body
typedef struct HEADSTR
{
Char magic[6];
Char Cmdcount;
int Infolen;
unsigned long crcvalue;
UINT Crcvalue;
}*pheadinfo,headinfo;//(6+1+1 (padded)) +4+4
std::string pheadinfo (Cheadinfo);
Std::string cmdpacket=pheadinfo.c_str;
After the struct is cast to char*, the extra space may be filled with \ s, so you should not use cout, or Qdebug to print the string, because you can only print out the characters that precede the first.
And do not use the. Length (), or. Size, that comes with C + +. Count your character length, so that it also counts the characters before you first (there may be a bit before you fill it), so you can't send out all the data
TCP network transmission, data type issues