Tutorials Point


  Learning Unix Socket
  Socket Quick Guide
  Socket Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Network Byte Orders


previous next AddThis Social Bookmark Button


Advertisements

Unfortunately, note all computers store the bytes that comprise a multibyte value in the same order. Consider a 16 bit interget that is made up of 2 bytes. There are two ways to store this value.

  • Little Endian: In this scheme low-order byte is stored on the starting address (A) and high-order byte is stored on the next address (A + 1).

  • Big Endian: In this scheme high-order byte is stored on the started address (A) and low-order byte is stored on the next address (A+1).

So that machines with different byte order conventions can communicate, the Internet protocols specify a canonical byte order convention for data transmitted over the network. This is known as Network Byte Order.

When establishing an Internet socket connection, you must make sure that the data in the sin_port and sin_addr members of the sockaddr_in structure are represented in Network Byte Order.

Byte Ordering Functions:

Routines for converting data between a host's internal representation and Network Byte Order are:

FunctionDescription
htons()Host to Network Short
htonl()Host to Network Long
ntohl()Network to Host Long
ntohs()Network to Host Short

Here is more detail of these functions:

  • unsigned short htons(unsigned short hostshort)
    This function converts 16-bit (2-byte) quantities from host byte order to network byte order.

  • unsigned long htonl(unsigned long hostlong)
    This function converts 32-bit (4-byte) quantities from host byte order to network byte order.

  • unsigned short ntohs(unsigned short netshort)
    This function converts 16-bit (2-byte) quantities from network byte order to host byte order.

  • unsigned long ntohl(unsigned long netlong)
    This function converts 32-bit quantities from network byte order to host byte order.

These functions are macros and result in the insertion of conversion source code into the calling program. On little-endian machines the code will change the values around to network byte order. On big-endian machines no code is inserted since none is needed; the functions are defined as null.

Program to determine host byte order:

Keep the following code in a file byteorder.c and then compile it and run it over your machine.

In this example we store the two-byte value 0x0102 in the short integer and then look at the two consecutive bytes, c[0] (the address A) and c[1] (the address A+1) to determine the byte order.

#include <stdio.h>

int main(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");
        else
            printf("unknown\n");
   } else{
        printf("sizeof(short) = %d\n", sizeof(short));
   }
   exit(0);
}

An output generated by this program on a pentium machine is as follows:

$> gcc byteorder.c
$> ./a.out
little-endian
$>

previous next Printer Friendly

Advertisements


  

Advertisements