Unix Sockets 简明教程

Unix Socket - Network Byte Orders

不幸的是,并非所有计算机都以相同的顺序存储构成多字节值的那个字节。考虑一个由 2 个字节组成的 16 位互联网。有两种方法来存储此值。

Unfortunately, not all computers store the bytes that comprise a multibyte value in the same order. Consider a 16-bit internet that is made up of 2 bytes. There are two ways to store this value.

  1. Little Endian − In this scheme, low-order byte is stored on the starting address (A) and high-order byte is stored on the next address (A + 1).

  2. Big Endian − In this scheme, high-order byte is stored on the starting address (A) and low-order byte is stored on the next address (A + 1).

为了允许具有不同字节顺序约定的机器互相通信,因特网协议为通过网络传输的数据指定了一个规范的字节顺序约定。这被称为网络字节顺序。

To allow machines with different byte order conventions communicate with each other, the Internet protocols specify a canonical byte order convention for data transmitted over the network. This is known as Network Byte Order.

在建立 Internet 套接字连接时,您必须确保 sockaddr_in 结构的 sin_port 和 sin_addr 成员中的数据以网络字节顺序表示。

While establishing an Internet socket connection, you must make sure that the data in the sin_port and sin_addr members of the sockaddr_in structure are represented in Network Byte Order.

Byte Ordering Functions

用于在主机内部表示和网络字节顺序之间转换数据的例程如下 -

Routines for converting data between a host’s internal representation and Network Byte Order are as follows −

Function

Description

htons()

Host to Network Short

htonl()

Host to Network Long

ntohl()

Network to Host Long

ntohs()

Network to Host Short

下面列出了一些有关这些函数的更多详细信息 −

Listed below are some more detail about these functions −

  1. unsigned short htons(unsigned short hostshort) − This function converts 16-bit (2-byte) quantities from host byte order to network byte order.

  2. unsigned long htonl(unsigned long hostlong) − This function converts 32-bit (4-byte) quantities from host byte order to network byte order.

  3. unsigned short ntohs(unsigned short netshort) − This function converts 16-bit (2-byte) quantities from network byte order to host byte order.

  4. unsigned long ntohl(unsigned long netlong) − This function converts 32-bit quantities from network byte order to host byte order.

这些函数是宏,并导致将转换源代码插入到调用程序中。在小端机器上,代码会将值更改为网络字节顺序。在大型机器上,不插入任何代码,因为不需要这些代码;这些函数被定义为 null。

These functions are macros and result in the insertion of conversion source code into the calling program. On little-endian machines, the code will change the values around to network byte order. On big-endian machines, no code is inserted since none is needed; the functions are defined as null.

Program to Determine Host Byte Order

将以下代码保存在一个名为 byteorder.c 的文件中,然后编译它并在机器上运行。

Keep the following code in a file byteorder.c and then compile it and run it over your machine.

在此示例中,我们将两个字节值 0x0102 存储在 short integer 中,然后查看两个连续的字节 c[0](地址 A)和 c[1](地址 A + 1)以确定字节顺序。

In this example, we store the two-byte value 0x0102 in the short integer and then look at the two consecutive bytes, c[0] (the address A) and c[1] (the address A + 1) to determine the byte order.

#include <stdio.h>

int main(int argc, char **argv) {

   union {
      short s;
      char c[sizeof(short)];
   }un;

   un.s = 0x0102;

   if (sizeof(short) == 2) {
      if (un.c[0] == 1 && un.c[1] == 2)
         printf("big-endian\n");

      else if (un.c[0] == 2 && un.c[1] == 1)
         printf("little-endian\n");

      else
         printf("unknown\n");
   }
   else {
      printf("sizeof(short) = %d\n", sizeof(short));
   }

   exit(0);
}

此程序在奔腾机器上生成的输出如下 -

An output generated by this program on a Pentium machine is as follows −

$> gcc byteorder.c
$> ./a.out
little-endian
$>