TCP network transmission, data type issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.