"UNIX Network programming (i)" Socket address structure, network byte order, and address translation capabilities

Source: Internet
Author: User
Tags htons

Description: Each socket address structure should be implemented in network programming. So the main socket address structure after the premise network planning, address structure can be sent in two directions: from the process to the kernel and kernel processing. Conversion between binary values in the structure.


Most socket functions require a pointer to the socket address structure as a parameter. different protocols have their own socket address structure.

The generic socket address structure is sockaddr. The IPV4 socket address structure is defined in the header file <netinet/in.h> sockaddr_in, and its POSIX definitions such as the following:

struct in_addr{in_addr_ts_addr;/*32-bit IPv4 address*/};/*network byte ordered*/struct sockaddr_in{unit8_tsin_len;/* Length of structure (+) */sa_family_tsin_family;/*af_inet*/in_port_tsin_port;/*16-bit TCP or UDP port number*//* Network byte ordered*/struct in_addrsin_addr;/*32-bit IPv4 address, network byte ordered*/charsin_zero[8];};
the POSIX specification requires only 3 fields in a structure: sin_family, sin_addr, and Sin_port.

Their data types are also given above.

Note: There are two different ways to access the 32-bit IPV4 address. For example, assuming that serv is defined as a network socket address structure, SERV.SIN_ADDR will refer to the 32-bit IPV4 address in the IN_ADDR structure. SERV.SIN_ADDR.S_ADDR will refer to the same 32-bit IPV4 address by in_addr_t (typically an unsigned 32-bit integer). Therefore, the IPV4 address must be used correctly. In particular, it is used as a function parameter. Because the compiler is completely different in the process of passing structures and passing integers.

The generic socket address structure SOCKADDR is defined in the <sys/socket.h> header file.

struct sockaddr{unit8_tsa_len;/*address family:af_xxx value*/sa_family_tsa_family;/*protocol-specific address*/};
A socket function is defined as a reference to a pointer to a generic socket address structure. such as int bind (int, struct sockaddr *, socklen_t); When you call these functions, you must cast a pointer to the protocol-specific socket address structure into a pointer to a generic socket address structure.  such as struct sockaddr_in serv; Bind (SOCKFD, (struct sockaddr *) &serv, sizeof (serv)); This technique is almost used in all socket functions and must be mastered.

There are 5 types of socket address structures: IPV4, IPv6, UNIX domains, data links, and storage.


An address translation function that converts an internetwork address between an ASCII string and a binary value of the network byte order.

Inet_aton, inet_addr (already unused) and Inet_ntoa convert the IPv4 address between dotted decimal string (such as "206.168.112.96") and its network byte-order binary value with a length of 32 bits. Inet_pton and Inet_ntop are applicable for both IPV4 and IPV6 addresses.

#include <arpa/inet.h>int inet_aton (const char *strptr, struct in_addr *addrptr);  /* Returns: 1 if the string is valid, otherwise 0*/char  *inet_ntoa (struct in_addr inaddr);/* Returns: pointer to a dotted decimal number string */
Inet_aton Converts the StrPtr string to a 32-bit network byte-order binary value and is stored by pointer addrptr. If the success is 1, otherwise 0. The Inet_ntoa function translates a 32-bit network byte-order binary IPV4 address into a corresponding dotted-decimal string, and the strings pointed to by the function's return value reside in static memory. Note that the function takes as its parameters a pointer to a structure other than the structure.

#include <arpa/inet.h>int inet_pton (int family, const char *strptr, void *addrptr); const char *inet_ntop (int family , const void *addrptr, char *strptr, size_t len);

The family parameters of two functions can be either af_inet or AF_INET6. Assume that the address family is not supported as a family parameter. The two functions return an error and place the errno eafnosupport.

The first function attempts to convert a string that is referred to by the StrPtr pointer and holds the binary result through the addrptr pointer. If successful, the return value is 1. Otherwise, it is assumed that the string entered for the specified family is not a valid expression format and returns 0. Inet_ntop converts to an expression from a numeric format to an inverse conversion.


Since the above mentions the network byte order, then must say the network byte order and the host byte order difference.

There are two ways to store two bytes in memory: One is to store the Di bytes in the start address, called the small endian, and the other is to store the high-end bytes at the start address. Called the big endian byte order.

The host byte order is the byte order used by the system and can be tested with the following program:

#include <stdio.h> #include <stdlib.h>intmain (int argc, char **argv) {union{short S;char  c[sizeof ( short)];} UN;UN.S = 0x0102;if (sizeof (short) = = 2) {if (un.c[0] = = 1 && un.c[1] = = 2) printf ("big-endian\n"); else if (un.c[0] = = 2 && un.c[1] = = 1) printf ("little-endian\n"); elseprintf ("unkonwn\n");} elseprintf ("sizeof (short) =%d\n", sizeof (short)); exit (0);}
The functions of the network byte order and the host byte order are converted to each other such as the following :

#include <netinet/in.h>

unit16_t htons (unit16_t host16bitvalue);

unit32_t htons (unit32_t host32bitvalue);/* Returns the value of the network byte order */

unit16_t Ntohs (unit16_t net16bitvalue);

unit32_t Ntohs (unit32_t net32bitvalue); /* Returns the host byte order value */

s should be treated as a 16-bit value (for example, TCP or Udpport). Consider L as a 32-bit value (e.g. IPV4 address);
In addition to the byte order problem for each field in the protocol header, there is a byte order problem with the data contained in the network grouping.


Add: Byte manipulation function

There are two groups of functions that manipulate multibyte fields: they neither interpret the data correctly nor if the data is a string that ends with a null character.

#include <string.h>

void bzero (void *dest, size_t nbytes);

void bcopy (const void *src, void *dest, size_t bytes);

int bcmp (const void *PTR1, const void *PTR2, size_t nbytes);

#include <string.h>

void *memset (void *dest, int c, size_t len);

void *memcpy (void *dest, const void *SRC, size_t nbytes);

int memcmp (const void *PTR1, const void *PTR2, size_t nbytes);



"UNIX Network programming (i)" Socket address structure, network byte order, and address translation capabilities

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.