Unix Sockets 简明教程

What is a Socket?

套接字允许同一台或不同机器上的两个进程之间通信。说得更精确一些,套接字是一种使用标准 Unix 文件描述符与其他计算机通信的方式。在 Unix 中,每个 I/O 操作都是通过写入或读取文件描述符来完成的。文件描述符只是一个与打开的文件相关联的整数,它可以是网络连接、文本文件、终端或其他东西。

Sockets allow communication between two different processes on the same or different machines. To be more precise, it’s a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else.

对于程序员来说,套接字的外观和行为与低级文件描述符非常相似。这是因为 read() 和 write() 等命令与套接字一起工作的原理与它们与文件和管道一起工作的原理相同。

To a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes.

套接字最早出现于 2.1BSD 中,随后在 4.2BSD 中被改进为当前形式。现在,大多数当前 UNIX 系统发行版都可以使用套接字功能。

Sockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases.

Where is Socket Used?

Unix 套接字用于客户端-服务器应用程序框架。服务器是一个根据客户端请求执行某些功能的进程。大多数应用程序级协议(如 FTP、SMTP 和 POP3)都使用套接字在客户端和服务器之间建立连接,然后交换数据。

A Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client and server and then for exchanging data.

Socket Types

用户可以使用四种类型的套接字。前两种最常用,后两种很少用。

There are four types of sockets available to the users. The first two are most commonly used and the last two are rarely used.

通常认为进程只能在同类型套接字之间进行通信,但是没有规定阻止不同类型套接字之间的通信。

Processes are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types.

  1. Stream Sockets − Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A, B, C", they will arrive in the same order − "A, B, C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries.

  2. Datagram Sockets − Delivery in a networked environment is not guaranteed. They’re connectionless because you don’t need to have an open connection as in Stream Sockets − you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).

  3. Raw Sockets − These provide users access to the underlying communication protocols, which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more cryptic facilities of an existing protocol.

  4. Sequenced Packet Sockets − They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as a part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets.

What is Next?

接下来的几章旨在加强你的基础知识,在你使用套接字编写服务器和客户端程序之前奠定基础。如果您想直接跳到如何编写客户端和服务器程序,您可以这样做,但这不是推荐做法。强烈建议你一步一步地进行,完成这些最初的几章,在你开始编程之前打好基础。

The next few chapters are meant to strengthen your basics and prepare a foundation before you can write Server and Client programs using socket. If you directly want to jump to see how to write a client and server program, then you can do so but it is not recommended. It is strongly recommended that you go step by step and complete these initial few chapters to make your base before moving on to do programming.