【发布时间】:2019-03-20 18:33:11
【问题描述】:
如 boost 文档中所述:
ip::basic_endpoint::port (1 of 2 overloads)
unsigned short port() const;
此 getter 获取与端点关联的端口。 端口号始终按主机的字节顺序排列。
我知道 Little-endian 字节排序将最低有效字节放在首位。 但是,Big-endian 字节排序将最重要的字节放在首位。
在 C 语言中,我们使用这些函数:
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
如何在 boost asio 中获取正确的端口号?
谢谢
【问题讨论】:
-
进一步思考:函数的命名很好地证实了我的回答:在 C 语言中,您可以调用
ntoh将网络字节顺序转换为主机字节顺序。如您所见,这里没有必要,因为这已经完成(该值保证按主机顺序排列,这是唯一“正确”的值 - 即使它在线路上看起来可能不同)。
标签: networking boost boost-asio