S Guide Network to Programming series serialization 05

Source: Internet
Author: User
Tags htons

3.2 byte order
Command from the King! There are only two types of byte order: Lame and Magnificent!

I'm kidding, but that's the truth. J

In fact, there are two types of computer storage: Big-Endian and Little-Endian ).

Computers of different architectures have different host sequences. For example, intel8080808080's host has a small byte sequence. Motorola's 68 K host sequence is large. The host sequence of PowerPC is... Wait!

How can we encapsulate the network's byte order?

Good news! You don't have to consider the details of these hosts in the byte sequence. We use some functions to handle this.

Okay. We have two types to convert them: short (two bytes) and long (four bytes ). They are all unsigned. If you want the host to be short in sequence, we use the htons () function. H indicates host, n indicates network, and s indicates short. (Read as: Host to Network Short)

How simple it is...

You can use any combination of them (n, h, s), but you cannot use the stolh () function (Short toLong Host )~~

Below are some common functions:

Htons () host to network short

Htonl () host to network long

Ntohs () network to host short

Ntohl () network to host long

3.3 struct
Finally speaking of programming. In this chapter, I will talk about various data types used by sockets. Because some of them are important.

The first is a simple one: socket descriptor. It is of the following type:

Int

It is just a common int.

From now on, things have become incredible, and what you need to do is to continue watching. Note the fact that there are two types of byte order: Important bytes (sometimes "octet", that is, octet) are in front or unimportant bytes are in front. The former is called "Network Byte Order )". Some machines store data in this order, while others do not. When I say that a data must be in the NBO Order, you need to call a function (such as htons () to convert it from the local Byte Order (Host Byte Order. If NBO is not mentioned, keep it in the local byte order.

My first structure (in this technical manual TM) -- struct addrinfo .. This structure provides the primary information for many types of socket storage sockets:

 

 

We can use the getaddrinfo () function to obtain a pointer to this structure.

This function is used to maintain the IPv4 to IPv6 guide.

However, the actual structure we use is structsockaddr.

Struct sockaddr {

Unsigned short sa_family; // address family, AF_xxx

Char sa_data [14] // 14 bytes of protocol address

};

Sa_family is AF_INET in IPv4 and AF_INET6 in IPv6.

Sa_data includes the address (IP) and Port (processed ).

Specifically, the programmer uses the structsockaddr_in structure. This structure is directly mapped to struct sockaddr.

 

 

Note that sin_zero can be set using the bzero () function. (Originally, memset ())

Sin_port must use htons ()!

Here, the structure declaration of struct in_addr is as follows:

// (IPv4 only-seestruct in6_addr for IPv6)

// Internetaddress (a structure for historical reasons)

Struct in_addr {

Uint32_t s_addr; // that's a 32-bit int (4 bytes)

};

It used to be the worst combination, but now those days have passed. If you declare that "ina" is an instance of the data structure struct sockaddr_in, "ina. sin_addr.s_addr" stores 4-byte IP addresses (in the byte network order ). If your unfortunate system is still using the horrible struct in_addr, you can rest assured that the 4-byte IP address is the same as I mentioned above (this is because "# define" is used ".)

What about IPv6? Below is:

 

 

Struct in6_addr {

Unsigned char s6_addr [16]; // IPv6address

}; Www.2cto.com

Let's look at another simple structure. struct sockaddr_storage can be used in both IPv4 and IPv6.

 

 

For the ss_family field, see AF_INET or AF_INET6 (IPv4 orIPv6 ). The ing structure is struct sockaddr_in or struct sockaddr_in6.

 

From the column xiaobin_HLJ80

Related Article

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.