Three representation formats of IP addresses and Their Applications in socket programming

Source: Internet
Author: User
Tags socket error htons

Author: huangguisu

If you are using the TCP/IP protocol for network application development, you must first process the IP address information. There are actually three different formats for IP addresses:

1) Ascii (network point string )-

2) network address (32-bit unsigned integer, network byte order, big data)

3) Host address (host byte)

An IP address is the basis for data transmission in an IP network. It identifies a connection in an IP network. A host can have multiple IP addresses, and the IP addresses in the IP Group will remain unchanged during network transmission. The following describes three different formats of IP addresses.

I. Point-to-10 representation format

This is our most common representation format. For example, the IP address of a host may be " ". In fact, for Ipv4 (IP version), the IP address is composed of a 32-bit binary number, but such a string of numbers is undoubtedly very lengthy and hard to read and remember. In order to facilitate people's memory and use, the series of numbers are divided into four groups, each group of 8 bits, represented by a 10-digit number, and finally separated by a small origin, as a result, it evolved into a "dot-10 hexadecimal representation format ".
Let's take a look at the specific conversion process of the IP Address:
Actual IP Address:11001010011001010110100101000010
Divided into four groups: 11001010 01100101 01101001
Decimal: 202 101 105 66

Point representation:

Ii. Network Byte Order format (NBO, Network Byte Order)

The Network byte sequence format is the same as the host byte sequence format, which is only encountered during network development. Therefore, in the following introduction, I assume that the reader has a certain foundation for Socket programming knowledge.
During network transmission, when the TCP/IP protocol saves the 32-bit binary number of the IP address, the Protocol specifies the storage order of the High-byte data contained in the low-level storage address (large-headed ), this ordered format is calledNetwork byte sequence format. During actual network transmission, data is transmitted in a set of 32-bit binary numbers. Due to the storage order, the actual byte transmission order is from the high byte to the low byte transmission order.
To enable both parties to understand the binary information such as the source address, destination address, and group length carried by the Data Group, whether it is a host or a router, before each group is sent, the binary information must be converted to the standard network byte sequence format of TCP/IP. The network address in the byte sequence format is not affected by the host or router type, and its representation is unique.

In socket programming and development, the functions inet_addr and inet_ntoa can be used to convert the dot string to the IP address in the network byte sequence format.
The following is a prototype of the inet_addr function:

  unsigned   long   inet_addr(const   char   FAR   *   cp)   

The CP parameter in the function points to the standard dot-split address string in the network. Each number separated by a dot cannot exceed 255, these numbers can be decimal, octal, hexadecimal, or mixed. For example, "", "", "0xa. 0x3. 0x14. 0x2", and " ".

We mentioned the client code in the previous socket programming and connected the local port:

/* File Name: client. c */# include <stdio. h> # include <stdlib. h> # include <string. h> # include <errno. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # define MAXLINE 4096int main (int argc, char ** argv) {int sockfd, n, rec_len; char recvline [4096], sendline [4096]; char buf [MAXLINE]; struct sockaddr_in servaddr; if (sockfd = socket (AF_INET, SOCK_STREAM, 0) <0) {printf ("create socket error: % s (errno: % d) \ n ", strerror (errno), errno); exit (0);} memset (& servaddr, 0, sizeof (servaddr); servaddr. sin_family = AF_INET; servaddr. sin_port = htons (8000); // available: inet_pton (AF_INET, "", servaddr. sin_addr); servaddr. sin_addr.s_addr = inet_addr (""); // converts an IP address in the string format to an integer value of connect (sockfd, (struct sockaddr *) & servaddr, sizeof (servaddr); printf ("send msg to server: \ n"); fgets (sendline, 4096, stdin); send (sockfd, sendline, strlen (sendline )); irec_len = recv (sockfd, buf, MAXLINE, 0); buf [rec_len] = '\ 0'; printf ("Received: % s", buf ); close (sockfd );}
Iii. Host Byte Order format (HBO, Host Byte Order)

The format of the host's byte sequence is as the name suggests. The IP address format is related to a specific host or router. For different hosts, there are different formats for storing IP addresses. For example, for Motorola 68k series hosts, HBO and nbo are the same. For intel X86 series, HBO and nbo are the opposite.
In socket programming, there are four functions to convert the host byte sequence format and network byte sequence format: htonl, htons, ntohl, and ntohs. Htons and ntohs convert each other to a 16-bit unsigned number. htonl and ntohl convert each other to a 32-bit unsigned number.
In practical applications, we often use the example of converting the port number (for example, above ). This is because if you enter a number and use this number as the port number, the application must use it before creating an address, it is converted from the host byte sequence to the network byte sequence (using the htons () function) to comply with the storage standards stipulated by the TCP/IP protocol. Correspondingly, if the application wants to display the port numbers contained in an address (for example, returned from the getpeername () function ), this port number must be converted from the network sequence to the host sequence before being displayed (using the ntohs () function ).
So what are the application of converting IP addresses and host byte sequence formats?
Application 1: How many host addresses are there between the two IP addresses and In this case, the two IP addresses can be converted to the host byte sequence format and then subtracted. The specific implementation is as follows:

int   GetIPCount(char   *   ip1,char   *   ip2)   {   long   pp;;   long   ss;;       pp   =   ntohl(inet_addr(ip1));;   ss   =   ntohl(inet_addr(ip2));;       return(ss   -   pp   +   1);;   } 

Application 2: If a network segment is scanned, for example, is being scanned, how can the program know that the next IP address to be scanned is In this case, you can convert the current IP address to the host byte sequence format and Add 1 to it.
The specific implementation is as follows:

  char   *   GetNextIp(char   *   m_curip)   {           struct   sockaddr_in   in;;           long   pp;;           char   *   re;;               pp   =   ntohl(inet_addr(m_curip));;           pp   =   pp   +   1;;               in.sin_addr.s_addr   =   htonl(pp);;           re   =   inet_ntoa(in.sin_addr);;            return   (re);;     }   


This article introduces three different IP address representation formats, including the causes and meanings of various formats and some applications in socket programming and development. In practical applications, the format standards used in applications must be followed. Meanwhile, the inter-format conversion and computing skills should be used flexibly.

Knowledge about byte sequence

1) byte order

The byte sequence is also called the end sequence and the end sequence. The English is endianness. In the field of computer science, byte refers to the bytes that store multi-byte data.Sequence, The typical situation is the storage method of integers in the memory andNetwork TransmissionOfTransmission sequence. Endianness sometimes can also use the point order (BIT ).

In general, the byte order indicates which byte of A UCS-2 character is stored at a low address. If lsbyte is in front of msbyte, that is, the LSB is a low address, the byte order is a small order; otherwise, it is a large order. InNetworkIn programming, the byte sequence is a factor that must be considered, because different processor systems may adopt different byte sequences. In multi-platform code programming, byte order may lead to bugs that are hard to detect.


The least significant
Bit, lsb
):A 0th-bit binary number (that is, a percentile bit) has a weight of 2 ^ 0. It can be used to detect the parity of the number. The opposite is the highest effective bit. In the big order, LSB refers to the rightmost bit.

Highest valid bit(The Most Significant Bit,Msb): It refers to the n-1 bits in an n-bit binary number with the highest weight. The opposite is called the least valid bit. In the big order, msb refers to the leftmost bits.

For signed binary numbers, negative numbers are in the form of reverse code or complementary code. In this case, msb is used to represent symbols. msb is 1 to represent negative numbers, and 0 to represent positive numbers.

Single-byte (Abyte): Most processors process bits in the same order. Therefore, the storage method and transmission method of a single byte are generally the same.

Multi-byte: for example, an integer (usually 4 bytes in 32-bit machines). A multi-Byte object is stored as a continuous byte sequence, and the data memory address is the minimum address of the memory address.

For example, the addresses of long data are ox001, ox002, ox003, and ox004. The data memory address is ox001.

There are two ways to store multi-byte data on different processors:

Big client Sequence(English name: big endian):Indicates that the data is stored from the highest bit. The highest digit is the first digit, that is, the high byte is stored in the memory low address, and the low byte is stored in the memory high address, objects are stored in sequence from the highest valid bytes to the lowest valid bytes.

Small client order(English name: little endian):The minimum number of digits is at the beginning. That is, the lower bytes are stored in the memory, the higher bytes are stored in the memory, from the lowest valid bytes to the highest valid bytes.SequenceStorage objects.

For example, a decimal number of 12345. 1 has the highest number of digits, which is ten thousand; 5 has the lowest number, which is a single digit.

In the big-End sequence, the memory starts from 12345 BITs, which is expressed;
If the sub-order is small, it starts from everybody and is expressed as 54321.

Another example is the storage representation of long data 0x12345678:

Large-end sequential storage representation:


Memory Address


Low memory address -->



A + 1



A + 2



High memory address --> a + 3



Storage representation of the Small-End sequence:


Memory Address


Low memory address -->



A + 1



A + 2



High memory address --> A + 3



2) network order

Network Transmission generally adopts the big-End sequence, which is also calledNetwork byte order, OrNetwork order. In the IP protocol, the large client order is defined as the network byte order.

SocketAPI defines a set of conversion functions for 16 and 32-bit integers in the network order andLocal byte order. Htonl and htons are used for local sequence conversion to network sequence; ntohl and ntohs are used for network sequence conversion to local sequence.

3) Order

It is generally used to describe the Transmission sequence of a serial device. Generally, hardware transmission adopts the small-End sequence (low-level first), but the I2C protocol adopts the large-End sequence. Only the bottom of the data link layer is involved in the network protocol.

4) processor system

1) small client orderSystem: X86, MOS
Technology 6502, Z80, VAX, PDP-11 and other processors for Littleendian.

2) Big client orderSystem: Motorola 6800, Motorola
68000, PowerPC 970, System/370

3) configurable: ARM, PowerPC (except PowerPC
970), DEC Alpha, SPARCV9, MIPS, PA-RISC and IA64 are configurable.

5) determine the large and small order by programming

#include <stdio.h>bool IsBigEndian(long a){    if(((char *)&a)[3] == 1)        return true ;    else        return false ;}void main(){    bool b ;    long a = 0x12345678;     b = IsBigEndian(a );printf("%d", &a);}

Open the Memory Window of VS to view the memory storage mode:

Set breakpoint:

Open debug --> window --> memory

View the address of variable A: 0x002bfe50

View the memory address:

From the above, we can see thatI am using x86, which is a small client sequence.

Verify that B = false is correct.

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: 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.