- 论坛徽章:
- 0
|
回复 #1 77h2_eleven 的帖子
- Network protocols specify a byte ordering so that heterogeneous computer systems can exchange protocol information without confusing the byte ordering. The TCP/IP protocol suite uses big-endian byte order. The byte ordering becomes visible to applications when they exchange formatted data. With TCP/IP, addresses are presented in network byte order, so applications sometimes need to translate them between the processor 's byte order and the network byte order. This is common when printing an address in a human-readable form, for example.
- Four common functions are provided to convert between the processor byte order and the network byte order for TCP/IP applications.
- #include <arpa/inet.h>
- uint32_t htonl(uint32_t hostint32);
- Returns: 32-bit integer in network byte order
-
- uint16_t htons(uint16_t hostint16);
- Returns: 16-bit integer in network byte order
-
- uint32_t ntohl(uint32_t netint32);
- Returns: 32-bit integer in host byte order
-
- uint16_t ntohs(uint16_t netint16);
- Returns: 16-bit integer in host byte order
复制代码
-- come from apue 2rd chapter 16.3.1 |
|