Network and host in Linux

Source: Internet
Author: User

The host byte sequence is what we usually call the big and small-end mode: different CPUs have different byte sequence types. These byte sequences refer to the order in which integers are stored in the memory. This is called the host sequence. Big-Endian and Little-Endian. The definitions of reference standard Big-Endian and Little-Endian are as follows:

A) Little-Endian is the low-byte emission at the low-address end of the memory, and the high-byte emission at the High-address end of the memory.

B) Big-Endian refers to the low address of the memory where the high byte is discharged, and the low byte is discharged to the high address of the memory.

Network byte order: the 32-bit values of four bytes are transmitted in the following order: the first is 0 ~ 7 bit, followed by 8 ~ 15bit, then 16 ~ 23bit, last 24 ~ 31bit. This transmission order is called the Large-end byte order. Because all the binary integers in the TCP/IP Header must be transmitted in this order, it is also called the network byte order. The byte order, as the name implies, is the order in which data of the same byte type is stored in the memory. The data of a single byte is not sequential.

The following four conversion functions are provided for bsd socket conversion:

Htons converts the unsigned short type from host to Network

Htonl converts the unsigned long type from the host sequence to the network Sequence

Ntohs converts the unsigned short type from the network sequence to the host Sequence

Ntohl converts the unsigned long type from the network sequence to the host Sequence

In systems using little endian, these functions convert the byte order.

In systems using the big endian type, these functions are defined as empty macros.

Note:

1. network and host byte Conversion Function: htons ntohs htonl ntohl (s means that short l is long h is host n is network)

2. different operating systems run on different CPUs, And the byte order is also different.
 

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.