gpt4 book ai didi

c - 为什么 8 位字段具有字节顺序?

转载 作者:太空狗 更新时间:2023-10-29 16:33:20 24 4
gpt4 key购买 nike

参见/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 */
};

为什么 8 位字段的字节顺序不同?我认为只有 16 位和 32 位字段与字节顺序有关,您可以分别使用 ntohs 和 ntohl 在字节顺序之间进行转换。处理 8 位事物的函数是什么?如果没有,则在小端机器上使用此 header 的 TCP 似乎无法与大端机器上的 TCP 一起工作。

最佳答案

有两种顺序。一种是字节序,一种是位域序。C语言中没有关于位域顺序的标准顺序。这取决于编译器。通常,位域的顺序在大端和小端之间是相反的。

关于c - 为什么 8 位字段具有字节顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/864077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com