Network programming problems often involve the transmission of binary data. The transports that are used frequently in C + + are text strings and grouping structures.
Assuming that the data can be pre-fed into contiguous memory areas, and then let the Send function get the first address, this contiguous memory area will be able to complete the transfer of data. A text string, such as Char, that is arranged in bytes. are stored sequentially in the. So it can be sent directly with the Send function.
However, assuming that multiple different types of data are sent at the same time, the addresses they store in memory are random, are not stored sequentially, and their relative positions cannot be determined. This requires a data organization to understand the relative position of each data. Structure is obviously a kind of data organization, the use of structural bodies to pay attention to the problem of data alignment, about the structure of data alignment problem can be taken into this article: http://blog.163.com/[email protected]/blog/ static/95532454200891191451827/
Assuming familiarity with the rules of data alignment in the structure, the structure of the structure can be reasonably designed, the order of member variables, so that all the data members are stored in a continuous storage area. And the length of the structure is equal to the sum of the length of all members (can be properly filled in the tail with a character array, to avoid the compiler itself to fill), so it is convenient to send with the Send function. (assuming that both the server and the client are developed in C/s + +, the two ends are able to package and package through the same structure, regardless of the data alignment), the following is a discussion of data transfer between the two ends of C + + and Python development: C + + written by client, The server side is written in Python. Python provides similar functionality for struct libraries, compared to C + + with structs, and the most important three functions are the detailed use of the Pack,unpack,calcsize,struct library to process binary data. This article: http ://www.cnblogs.com/gala/archive/2011/09/22/2184801.html. The byte stream received with the socket under Python is actually a string. You need to specify the parsing format for all the bytes, so you should avoid sending indeterminate data (such as data that the compiler is actively populating) when sending in C + +. It is assumed that data alignment should be considered when organizing data with structs.
This is illustrated by an example: in C++client there are 1 a (float), 1 b (unsigned char), a C (short) to be sent to the server side, without using the #pragma directive to specify the number of aligned bits of the compiler, In my compiler environment, I feel 4. The ability to design structures like this:
struct data{ float A; 0~3 short C; unsigned char b; 6 Char extra[2]; The 7~8 structure has a length of 8 bytes, where the character array is used to fill 8 bytes to avoid compiler padding};
The pythonserver end uses a struct library to unpack the package. Ability to design such as the following format:
Import Struct...rdata = S.recv (1024x768) a,b,c,d = struct. (' Fhb2s ', rdata) #a, B,c needs to print the data a,b,c
C + + and Python use structs to transmit binary data structures to implement