Several other routines that simplify manipulating names and addresses are available. The following table summarizes the routines for manipulating variable-length byte strings and byte-swapping network addresses and values.
Table 8–2 Runtime Library Routines
Interface |
Synopsis |
---|---|
Compares byte-strings; 0 if same, not 0 otherwise |
|
Copies n bytes from s2 to s1 |
|
Sets n bytes to value starting at base |
|
32-bit quantity from host into network byte order |
|
16-bit quantity from host into network byte order |
|
32-bit quantity from network into host byte order |
|
16-bit quantity from network into host byte order |
The byte-swapping routines are provided because the operating system expects addresses to be supplied in network order. On some architectures, the host byte ordering is different from network byte order, so programs must sometimes byte-swap values. Routines that return network addresses do so in network order. Byte-swapping problems occur only when interpreting network addresses. For example, the following code formats a TCP or UDP port:
printf("port number %d\n", ntohs(sp->s_port));
On machines that do not need these routines, the routines are defined as null macros.