Not all computers store bytes that make up multibyte values in the same order. Let's consider a 16-bit Internet consisting of 2 bytes. There are two ways to store this value.
In order to allow machines with different byte ordering rules to communicate with each other, the Internet protocol specifies a standard ordering rule for data sent over the network. This is called network byte order.
When establishing an Internet socket connection, make sure that the data for the sin_port and sin_addr members of the sockaddr_in structure is represented by the network byte order.
The routines for converting data between the internal representation of the host and the network byte order are as follows :
|htons()||Host to Network Short|
|htonl()||Host to Network Long|
|ntohl()||Network to Host Long|
|ntohs()||Network to Host Short|
The details of these functions are shown below:
These functions are macros, and insert the source code into the calling program. On small endian machines, the code changes the value around the network byte order. On the big endian machine, there is no need to insert code, the function is defined as null.
Save the following code in the byteorder.c file, compile it and run it on the machine.
In this example, we store the 2-byte value 0x0102 in a short integer, checking the two consecutive bytes c  (address A) and c  (address A + 1) and the order byte.
The program generates output on the Pentium machine as follows.
Unix socket Related Interview Questions
|Networking Interview Questions||UNIX/XENIX Interview Questions|
|Java-Multithreading Interview Questions||WebSockets Interview Questions|
|Socket Programming Interview Questions||Unix Inter-Process Communication (IPC) Interview Questions|
|UNIX Memory Management Interview Questions||UNIX Process Management Interview Questions|
|Advanced C++ Interview Questions||Basic C Interview Questions|
|Unix/Linux Interview Questions||Unix Production Support Interview Questions|
|Secure Socket Layer Interview Questions||Unix Shell Scripting Interview Questions|
|Angular 6 Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.