Cla*_*diu 15 c networking interop system endianness
请参阅/netinet/tcp.h中TCP头的定义:
struct tcphdr
{
u_int16_t th_sport; /* source port */
u_int16_t th_dport; /* destination port */
tcp_seq th_seq; /* sequence number */
tcp_seq th_ack; /* acknowledgement number */
# if __BYTE_ORDER == __LITTLE_ENDIAN
u_int8_t th_x2:4; /* (unused) */
u_int8_t th_off:4; /* data offset */
# endif
# if __BYTE_ORDER == __BIG_ENDIAN
u_int8_t th_off:4; /* data offset */
u_int8_t th_x2:4; /* (unused) */
# endif
u_int8_t th_flags;
# define TH_FIN 0x01
# define TH_SYN 0x02
# define TH_RST 0x04
# define TH_PUSH 0x08
# define TH_ACK 0x10
# define TH_URG 0x20
u_int16_t th_win; /* window */
u_int16_t th_sum; /* checksum */
u_int16_t th_urp; /* urgent pointer */
};
Run Code Online (Sandbox Code Playgroud)
为什么8位字段的字节顺序不同?我认为只有16位和32位字段与字节顺序相关,你可以分别用ntoh和ntohl在字节之间进行转换.处理8位内容的功能是什么?如果没有,似乎在小端机器上使用此标头的TCP将无法在大端机器上使用TCP.