V2EX  ›  英汉词典
Enqueued related words: Endian

Ntohs

释义 Definition

ntohs 是 C/C++ 网络编程中常见的函数/宏名,表示 “network to host short”:把16 位(short)的端口号等数值从网络字节序(大端序)转换为主机字节序,以便在不同 CPU 架构(大小端不同)上正确解释数据。常用于读取 TCP/UDP 头部中的端口号字段。
(同类还有 ntohl 用于 32 位。)

发音 Pronunciation (IPA)

/ˌɛn tiː oʊ eɪtʃ ˈɛs/

例句 Examples

The code uses ntohs to read the port number correctly.
这段代码使用 ntohs 来正确读取端口号。

After receiving a packet, the program converts the 16-bit fields with ntohs before logging them, ensuring the values are correct on both little-endian and big-endian systems.
接收数据包后,程序在记录日志前用 ntohs 转换这些 16 位字段,确保在小端和大端系统上数值都正确。

词源 Etymology

ntohs 来自缩写 n(network) + to + h(host) + s(short)。它属于 BSD sockets 传统接口的一部分:网络协议规定多字节整数通常使用网络字节序(大端),而主机可能是小端或大端,因此需要显式转换。

相关词 Related Words

文学与经典作品 Literary Works

  • UNIX Network Programming(W. Richard Stevens):在讲解字节序与 sockets API 时会频繁出现 ntohs/htons
  • TCP/IP Illustrated, Volume 1(W. Richard Stevens):讨论协议字段与网络字节序时常引用相关转换。
  • Beej’s Guide to Network Programming(Beej Jorgensen):入门网络编程教程中用 ntohs 解析端口号是典型示例。
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1363 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 16:15 · PVG 00:15 · LAX 08:15 · JFK 11:15
♥ Do have faith in what you're doing.