| < Day Day Up > |
|
The host and network byte order conversion functions are used to convert short and long values between host and network byte order. The function prototypes are:
#include <netinet/in.h> unsigned int htonl( unsigned int hostlong ); unsigned short int htons ( unsigned short hostshort ); unsigned int ntohl( unsigned int netlong ); unsigned short int ntohs( unsigned short netshort );
In the functions, “h” represents host and “n” represents network. Therefore, htonl represents host-to-network-long. Developers are required to use these functions for portability reasons. If an application runs on a processor that is big endian, then the functions are optimized away (as they perform no function), but the functions should still be included so that the application can be ported to a little-endian architecture without any issues.
Developers commonly use these functions when specifying the port number (for the sin_port field of sockaddr_in), as well as raw IPv4 address structure manipulation. In the next section, address functions are shown, which perform these functions internally so that the addresses are in the proper byte order for the given architecture.
| < Day Day Up > |
|