arpa_inet.h - Linux
Overview
arpa_inet.h is a header file that contains definitions for the arpa/inet.h library. It includes the standard definitions for internet operations and the following operations.
- int htons (short x): Convert a short integer from host byte order to network byte order (big-endian).
- short ntohs (short x): Convert a short integer from network byte order to host byte order (little-endian).
- long int htonl (long int x): Convert a long integer from host byte order to network byte order (big-endian).
- long int ntohl (long int x): Convert a long integer from network byte order to host byte order (little-endian).
Syntax
#include <arpa/inet.h>
Options / Flags
There are no options or flags associated with this library.
Examples
1. Convert a short integer from host byte order to network byte order.
#include <arpa/inet.h>
int main()
{
short x = 0x1234;
short y = htons(x);
printf("Host byte order: %x\n", x);
printf("Network byte order: %x\n", y);
return 0;
}
2. Convert a long integer from network byte order to host byte order.
#include <arpa/inet.h>
int main()
{
long int x = 0x12345678;
long int y = ntohl(x);
printf("Network byte order: %lx\n", x);
printf("Host byte order: %lx\n", y);
return 0;
}
Common Issues
- None reported.
Integration
This library can be used with other Linux commands or tools to perform advanced tasks. For example, it can be used with the socket() function to create a socket and specify the network byte order.
Related Commands
- socket() – Create a socket
- bind() – Bind a socket to an address
- listen() – Listen for connections on a socket
- accept() – Accept a connection on a socket
- connect() – Connect to a socket
- send() – Send data over a socket
- recv() – Receive data over a socket
- close() – Close a socket
Further Reading
- arpa/inet.h – man page for arpa/inet.h
- Network Programming in C – tutorial on network programming in C