Linux Network Programming--Network byte order, address translation

Source: Internet
Author: User
Tags 04x htons

Network byte order

The origins of the storyThe word "endian" is derived from Gulliver's Travels. The civil war in the small country stems from eating eggs is whether from the Big Head (Big-endian) or from the head (Little-endian) knocked Open, which has happened six times rebellion, one of the Emperor sent life, the other lost the throne.


We generally translate "endian" into "byte-order", and Big-endian and Little-endian are called "big-endian" and "small-format".


byte order

byte order refers to the storage order of multi-byte data, in the design of computer systems, there are two ways to deal with in-memory data: Big-endian format, small-end format.


Small-end format (Little-endian): Stores low-bit byte data at a lower address.
Big -endian format (Big-endian): Stores high-bit byte data at a low address.



For a simple example, for an orthopedic 0x12345678, it is stored in a system of big-endian and small-ended formats, as shown in the following way:


The following example determines the byte order of the host:

#include <stdio.h>int main (int argc, char *argv[]) {unsigned int a = 0x12345678;unsigned char *p = (unsigned char *) & amp;a;//take only one byte if (0x12 = = *p) {printf ("big-endian\n");} else if (0x78 = = *p) {printf ("little-endian\n");} return 0;}


the traffic on the network is a byte stream, for a multi-byte value, in the network transmission, the first pass which bytes? In other words, when the receiver receives the first byte, does it handle the byte as a high or low?

Network byte order definition: The first byte received is treated as a high level, which requires that the first byte sent by the sending side should be high. When sending data on the sending side, the first byte sent is the byte that corresponds to the number in the in-memory start address. Visible multibyte values before sending, in-memory values should be stored in the big-endian method.

Therefore, the network protocol specifies the communication byte order: big-endian. Only when multibyte data processing needs to consider the byte order, the process of running on the same computer to communicate with each other, generally do not consider the byte order, communication between heterogeneous computers, the need to convert their own byte order network byte order.

Introduction to Byte-order conversion functions

header files required for the following interfaces : #include <arpa/inet.h>


uint32_t htonl (uint32_t hostint32);

features :

Converting 32-bit host byte-order data to network byte-order data

Parameters :

Hostint32: 32-bit host byte-order data that needs to be converted, uint32_t to 32 as unsigned integer

return value :

Success: Returns the value of the network byte order


uint16_t htons (uint16_t hostint16);

features :

Converting 16-bit host byte-order data to network byte-order data

Parameters :

hostint16: 16-bit host byte-order data that needs to be converted, uint16_t,unsigned short int

return value :

Success: Returns the value of the network byte order


Test Example:

#include <stdio.h> #include <arpa/inet.h>int main (int argc, char *argv[]) {int a = 0x01020304;short int b = 0x01 02;printf ("htonl (0x%08x) = 0x%08x\n", A, htonl (a));p rintf ("htons (0x%04x) = 0x%04x\n", B, htons (b)); return 0;}
The results of the operation are as follows:




uint32_t Ntohl (uint32_t netint32);

features :

Converts 32-bit network byte-order data to host byte-order data

Parameters :

Netint32: 32-bit network byte-order data to be converted, uint32_t,unsigned int

return value :

Success: Returns the value of the host byte order


uint16_t Ntohs (uint16_t netint16);

features :

Converts 16-bit network byte-order data to host byte-order data

Parameters :

netint16: 16-bit network byte-order data to be converted, uint16_t,unsigned short int

return value :

Success: Returns the value of the host byte order


Address Translation Functions header files required for the following interfaces : #include <arpa/inet.h>

int Inet_pton (int family, const char *strptr, void *addrptr);

features :

Convert dotted decimal string to 32-bit unsigned integer

Parameters :

Family: protocol family ( af_inet, Af_inet6, Pf_packet, etc.), commonly used af_inet

strptr: Dotted decimal number string

addrptr: Address of 32-bit unsigned integer

return value :

Successfully returned 1, failed to return other

Test Example:

#include <stdio.h> #include <arpa/inet.h>int main () {char ip_str[]= "172.20.223.75"; unsigned int ip_uint = 0; unsigned char *ip_p = Null;inet_pton (af_inet,ip_str,&ip_uint);p rintf ("In_uint =%d\n", ip_uint); ip_p = (char *) & ip_uint;printf ("In_uint =%d,%d,%d,%d\n", *ip_p,* (ip_p+1), * (ip_p+2), * (ip_p+3)); return 0;}
The results of the operation are as follows:



const char *inet_ntop (int family,

const void *addrptr,   

char *strptr, 

size_t len);

features :

Converts a 32-bit unsigned integer into a dotted decimal number string

Parameters :

Family: protocol family (Af_inet, Af_inet6, Pf_packet, etc.), commonly used af_inet

addrptr: 32-bit unsigned integer

strptr: Dotted decimal number string

len: StrPtr buffer length

Len's macro definition
#define Inet_addrstrlen//For IPv4

#define Inet6_addrstrlen//For IPv6

return value :

Success: Returns the first address of the string

Failed: returned NULL

Test Example:

#include <stdio.h> #include <arpa/inet.h>int main () {unsigned char ip[] = {172,20,223,75};char ip_str[16] = " NULL "; Inet_ntop (af_inet, (unsigned int *) ip,ip_str,16);p rintf (" ip_str =%s\n ", ip_str); return 0;}
The results of the operation are as follows:



To test the sample code download please click here.

Linux Network Programming--Network byte order, address translation

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.