"Network" IP address format conversion (htonl, NTOHL;INET_ADDR, Inet_ntoa)

Source: Internet
Author: User
Tags htons

1, htonl () and Ntohl ()

U_long PASCAL far Ntohl (U_long Netlong);

U_short PASCAL far Ntohs (U_short netshort);

Ntohl ()-----Network order into host order

U_long PASCAL far htonl (U_long Hostlong);

U_short PASCAL far htons (U_short hostshort);

HTONL ()-----host sequence into network order

2, Inet_addr () and Inet_ntoa ()

unsigned long PASCAL far inet_addr (const char FAR * CP);

Char FAR * PASCAL far inet_ntoa (struct in_addr in);

The INET_ADDR function requires a string as its argument, which specifies an IP address in dotted decimal format (for example: 192.168.0.16). and the INET_ADDR function returns a value that is appropriate for the type of u_long assigned to S_ADDR.

The Inet_ntoa function completes the opposite conversion, takes a parameter of a in_addr struct type, and returns a string of IP addresses in dotted decimal format.

Sockaddr_in, sockaddr, in_addr difference

struct SOCKADDR {
unsigned short sa_family;
Char sa_data[14];
};
The above is a generic socket address, specific to the Internet socket, with the following structure, the two can be type conversion

struct SOCKADDR_IN {
short int sin_family;
unsigned short int sin_port;
struct IN_ADDR sin_addr;
unsigned char sin_zero[8];
};
A struct in_addr is a 32-bit IP address.
struct IN_ADDR {
Union {
struct {U_char s_b1,s_b2,s_b3,s_b4;} S_un_b;
struct {u_short s_w1,s_w2;} S_un_w;
U_long s_addr;
} S_un;

#define S_ADDR S_un. S_addr
};
Inet_addr () is the conversion of a point-and-click IP address (such as 192.168.0.1) to the 32-bit IP address (0xc0a80001) required in the above structure.

When the value is filled with the sockaddr_in structure, and as a function (such as socket, listen, bind, etc.) parameters are passed into the SOCKADDR structure on the line, after all, are 16 characters long.

The usual usage is:   
  int   sockfd;  
  struct   sockaddr_in    my_addr;  
  sockfd   =   socket (af_inet,   sock_ stream,   0);     
   
  my_addr.sin_family   =    af_inet;     
  my_addr.sin_port   =   htons (myport);      
  my_addr.sin_addr.s_addr   =   inet_addr ("192.168.0.1");   
   
  bzero (& (My_addr.sin_zero),   8);      
    
  bind (sockfd,   (struct   sockaddr   *) &my _addr,   sizeof (struct   sockaddr));

You can use C + + to make a less accurate assumption.
SOCKADDR is a base class
Sockaddr_in and so is derived class.
As a result, bind, connect, SendTo, recvfrom and other functions can use the base class
To handle a variety of different derived class.
But actually, this is not an inheritance data structure (c), so you need to force the styling to transform the data type. Because of this, Len lengths need to be given at SendTo because different SOCKADDR_XX implementations are not of the same length.

Noun Analysis:

Host byte order:

Different CPUs have different byte-order types, which are the order in which integers are stored in memory, called the host order. The most common are two kinds of 1. Little endian: Low byte storage high address, high byte storage low address 2. Big endian: Low byte memory low address, high byte memory high address

Network byte order:

Network byte order is a well-defined data representation format in TCP/IP, which is independent of the specific CPU type, operating system and so on, so that the data can be interpreted correctly when transferring between different hosts. The network byte order takes the big endian sort method.

In order to convert the BSD socket provides a function of conversion, there are four network and host byte conversion functions: Htons ntohs htonl Ntohl (S is short L is long H is host n is network)

Htons the unsigned short type from the host to the network sequence, htonl the unsigned long type from the host to the network sequence, ntohs the unsigned short type from the network sequence to the host sequence, ntohl the unsigned The long type is converted from the network order to the host order.

In systems that use little endian, these functions convert the byte order into a system that uses the big endian type, and these functions are defined as empty macros.

The address of a IN_ADDR structure will be converted with a dot-separated IP address, and the definition of this structure is shown in note (a), which is actually a unsigned long value. The computer handles IP addresses internally but does not recognize data such as 192.1.8.84.
unsigned long inet_addr (const char FAR * CP);
Example: inet_addr ("192.1.8.84") =1409810880
Inet_addr ("127.0.0.1") = 16777343

If an error occurs, the function returns the Inaddr_none value.
The IP address that divides the network address translation bits with points is the inverse function of the above function.
Char FAR * INET_NTOA (struct in_addr in);
Example: char * ipaddr=null;
Char addr[20];
IN_ADDR inaddr;
Inaddr. s_addr=16777343;
Ipaddr= Inet_ntoa (INADDR);
strcpy (ADDR,IPADDR);
The value of this addr becomes 127.0.0.1.
Be careful not to modify the return value or make the release action, and return a null value if the function fails.

"Network" IP address format conversion (htonl, NTOHL;INET_ADDR, Inet_ntoa)

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.