Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Converting the byte order

&pagelevel(3)&pagelevel

If you use the address conversion functions described above, you will seldom have to directly handle addresses in an Internet user program. You can then develop services that are independent of networks to a large extent. However, some network dependency still remains, since the IP address has to be specified in a user program if a name is assigned to a service or socket.

Besides the library functions for converting names to addresses, there are also some macros which simplify the handling of names and addresses.

The host byte order and network byte order differ in some architectures. Because of this, programs sometimes have to change the byte order. The macros summarized in the table below convert bytes and integers from host byte order to network byte order, and vice versa.

Library macros for converting byte orders

Call

Meaning

htonl(val)
htons(val)
ntohl(val)
ntohs(val)

Convert 32-bit fields from host byte order to network byte order
Convert 16-bit fields from host to network byte order
Convert 32-bit fields from network to host byte order
Convert 16-bit fields from network to host byte order

The byte order conversion macros are needed because the operating system expects the IPv4 addresses in network byte order. The library functions which return network addresses supply them in network byte order, allowing them to be simply copied into the structures available to the system. You should therefore only encounter byte order problems when interpreting network addresses.

The host and network byte orders are identical in BS2000. The macros listed in the table are therefore defined as null macros (macros without contents). However, it is strongly recommended that you use the macros if you want to create portable programs.

In IPv6 implementation, network addresses are always expected in network byte order, i.e. there is no definition of a difference between host byte order and network byte order, and there is therefore no corresponding conversion function.
If necessary, only the port number has to be converted.