TCP/IP address format conversion API

Source: Internet
Author: User
Tags htons

1, htonl () and Ntohl ()

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

U_long PASCAL far Ntohl (U_long Netlong);

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

U_long PASCAL far htonl (U_long Hostlong);

2, htons () and Ntohs ()

Htons ()------host sequence converted to network order (short integer type)

U_short PASCAL far htons (U_short hostshort);

NTOHS ()------Network order converted to host order (short integer type)

U_short PASCAL far Ntohs (U_short netshort);

3, 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.1.161). and the INET_ADDR function returns a value that is appropriate for the type of u_long assigned to S_ADDR.

intsockfd;structsockaddr_in my_addr; SOCKFD= Socket (Af_inet, Sock_stream,0); My_addr.sin_family= Af_inet;/*Host byte order*/My_addr.sin_port= Htons (9925);/*Short , network byte order*/my_addr.sin_addr.s_addr = inet_addr ("192.168.0.1"); Bzero (& (My_addr.sin_zero),8); Bind (SOCKFD, (structSOCKADDR *) &my_addr,sizeof(structSOCKADDR));

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.

After the server accept receives a connection, it can find the other IP by Inet_ntoa, and output the dotted decimal in string format.

Client = Accept (ServerSocket, (structSOCKADDR *) &clientaddr, (socklen_t *) &Addr_len); if(Client <0) {perror ("Accept");         }        printf ("\NRECV Client data...\n"); printf ("IP is%s\n", Inet_ntoa (CLIENTADDR.SIN_ADDR)); 
Sockaddr_in, sockaddr, in_addr difference

struct   sockaddr   {  
                 unsigned   short   sa_family;      
                char    sa_data[14];     
       };  

  above is a generic socket address, specific to the internet   socket, with the following structure, the two can be type conversion   
    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP
  struct   sockaddr_in   {  
                 short   int    sin_family;     
                 unsigned   short   int   sin_port;      
                struct    in_addr   sin_addr;     
                 unsigned   char   sin_zero[8];     
       };  
   

The

    struct   in_addr is a 32-bit IP address. &NBSP;&NBSP,
        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.

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. Two of the most common

1. Little endian: Low byte storage high address, high byte low memory 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

TCP/IP address format conversion API

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.