Summary of network data (socket) Transmission

Source: Internet
Author: User

Environment restrictions: Socket network transmission under TCP/IP; C/C ++ development language, 32/64-bit machine.

Currently, there are two methods for data transmission:1) Refer stream format, that is, data is represented by strings; 2) Structured mode, that is, data is directly transmitted by type.

(1) The method ensures that all data is clear string plain text without platform inconsistency, but the transmitted data is not long, and the analysis of composite data types (such as struct) is inconvenient.
2) ensures the data length is fixed and controllable, and facilitates data parsing. However, the premise is that platform inconsistencies, such as the byte order, alignment width, and data type, must be considered;

Note:
1) pipeline stream format-you need to negotiate the data parsing method, considering the possible problems caused by data uncertainty.
2) structure-mainly pay attention to problems caused by inconsistent platforms.

The primary stream transmission mode is relatively simple and secure, while the structured transmission mode is different. When transmitting data in the structure, check the following points:
1) First, check whether there are inconsistent machine bit widths, such as 32-bit and 64-bit machines. If the bit width is inconsistent, avoid inconsistent data types in the transmission representation mode. For example, if the long and float types are inconsistent in the 32-bit 64-bit representation mode, data parsing errors may occur.
2) Confirm that the alignment width of both parties is consistent, or ensure that the data structure transmitted is not inconsistent after alignment and adjustment. If the alignment between the receiving and receiving sides is 4-byte and 8-byte, You need to specify the alignment width in the program; or adjust the data structure to ensure that after alignment adjustment, the offset of each item in the received/sent data is the same.
3) try to convert the outgoing data to the network byte sequence and convert the received data back to the local byte sequence, especially when the machine's byte sequence is inconsistent, the existing byte sequence conversion functions cannot meet all requirements.
4) Some data types have different representation methods, such as floating-point numbers (generally in the IEEE 794 standard). It is not considered that the 8-byte double type with the correct byte order is correct, different platforms have different Representation Methods for the double type. For example, if sending 1.003, the acceptance is OK, but other values are obtained.

Processing of the byte sequence and floating point number when transmitting structured data. There is not much data on the Internet at the following two points. Please record them in detail:
1)Currently, there is no conversion function for 8-byte or Above data types. You must implement this function by yourself.There are many online websites. The following is an example:
Uint64_t htonll (uint64_t N ){
Return (uint64_t) htonl (N) <32) | htonl (n> 32 );
}
Uint64_t ntohll (uint64_t N ){
Return (uint64_t) ntohl (N) <32) | ntohl (n> 32 );
}
If there is longer data (12 bytes...), and so on.

2)For data such as floating-point numbers, if they are completely accurate, they are sent using strings. If data is directly transmitted, you still need to process the byte sequence, and ensure that the sending and receiving sides have the same way of representing the floating point number.The following are two double-type bytecode conversion functions:
Double ntoh_double (double net_double ){
Uint64_t host_int64;
Host_int64 = ntohll (* (uint64_t *) & net_double ));
Return * (double *) & host_int64 );
}

Double hton_double (double host_double ){
Uint64_t net_int64;
Net_int64 = htonll (* (uint64_t *) & host_double ));
Return * (double *) & net_int64 );
}
If the above function is not directly converted to uint64_t for host_double/net_double, data will be truncated.

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.