1.8 Byte order and packet formats
Last updated
Last updated
uint16_t http_port = 80; //Host Order
if (packet->port == host_port) { //Network vs. host order
...
}```htons()```:host to network short 表示将16位的主机字节顺序转化为网络字节顺序
```htonl()```:host to network long 表示将32位的主机字节顺序转化为网络字节顺序
```ntohs()```: network to host short
```ntohl()```: network to host long
```c
#include <arpa/inet.h>
uint_16_t http_port = 80;
uint_16_t packet_port = ntohs(packet->port); //ntohs()是一个函数名,作用是将一个16位数由网络字节顺序转换为主机字节顺序
if (packet_port == http_port) { //OK
...
}