Socket Address API

Source: Internet
Author: User

Socket 1. Host byte order and network byte order background: Modern CPU accumulators can load (at least) 4 bytes (32-bit machines) at a time. The order in which the 4 bytes are arranged in the * * memory will affect the value that it is loaded into an integer by the accumulator. Tip: The byte order is divided into big endian byte order and small end byte order. 1.1big endian: The high byte (23-31bit) of an integer is stored at the low address of memory, and the low bit byte (0-7bit) is stored at the high address of the memory. 1.2little endian: The high byte of an integer is stored at the higher address of the memory, while the lower byte is stored at the low address of memory at the following code: void Byteorder () {union{short value; char union_bytes [sizeof ( Short)]; } test; Test.value = 0x0102; if ((test.union_bytes[0] = = 1) && (test.union_bytes[1] = = 2)) {printf ("big Endian");} else if ((test.union_bytes[0] = = 2) && (test.union_bytes[1] = = 1)) {printf ("Ittle");} else{printf ("Unknown");}} 1.3 Modern PCs mostly use small byte sequences, so the small-endian byte-order is also called the host byte order. 2. When the formatted data (32bit or 16bit) A problem occurs between two hosts with different byte-order 2.1 solution: The sending side always converts the data to be sent to big endian in the send, the receiver knows that the data is always big endian byte order, so the receiver can decide whether to convert (small segment conversion, big endian byte sequence does not convert) according to its own situation 3. Endian byte order Network byte order 4. Process byte problem: two processes on the same machine, one in the C language, one in the Java language, and a byte order (Java Virtual machine generally uses big endian) 5.linux convert size end byte order function (4) 5.1 unsigned long int htonl (unsigned long int hostlong) 5.2 unsigned short int htons (unsigned short int hostshort); 5.3 unsigned long int nto HL (unsigned long int netlong); 5.4 Unsigned shoRT int Ntohs (unsigned short int netshort); 5.5 The above four meanings are quite clear: the htonl indicates that the host to network long is about to convert a long integer (32bit) master byte sequence into a net byte-order data. 5.6 Long integer functions are commonly used to convert IP addresses, and short integers are commonly used to convert port numbers (not limited to this, all formatted data should use these functions to convert the byte order when transmitting over the network)

Socket Address API

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.