# 伯克利套接字

**伯克利套接字**（Berkeley sockets）是一种用于网络通信的应用程序编程接口，于20世纪80年代初随4.2BSD Unix一同引入。在该接口中，程序创建一个称为套接字的端点，通过从中读取和向其写入来进行通信，方式与读写文件颇为相似。在UNIX环境中，网络API主要是BSD套接字，而该接口的采用范围远远超出了BSD，包括Windows的Winsock实现和POSIX标准。<sup>[1](https://people.eecs.berkeley.edu/~culler/WEI/labs/lab5-sockets/sockets-intro.html)</sup><sup> • </sup><sup>[2](https://cs.brown.edu/people/slewando/files/IPCWinNTUNIX.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 在4.2BSD中，对TCP/IP的访问通过套接字接口实现；1981年的一份伯克利报告（CSRG TR/4）包含了后来成为套接字API的初稿<sup>[3](https://www.researchgate.net/publication/2806930_Packet-oriented_Communication_Using_a_Stream_Protocol_or_Making_TCPIP_on_Berkeley_UNIX_a_little_more_pleasant_to_use)</sup><sup> • </sup><sup>[4](https://elists.isoc.org/pipermail/internet-history/2017-November/004447.html)</sup> |
| 核心调用 | socket、bind、listen、accept（服务端）；connect（客户端）；send和recv用于数据传输<sup>[2](https://cs.brown.edu/people/slewando/files/IPCWinNTUNIX.pdf)</sup> |
| 范围 | 套接字承载TCP/UDP/IP流量，也与I/O和设备驱动等系统级组件通信<sup>[1](https://people.eecs.berkeley.edu/~culler/WEI/labs/lab5-sockets/sockets-intro.html)</sup> |
| Windows移植 | Winsock的设计目的是便于伯克利套接字代码的轻松移植；WinSock v2.2支持伯克利API（有少量例外）并增加了Win32特有功能<sup>[2](https://cs.brown.edu/people/slewando/files/IPCWinNTUNIX.pdf)</sup> |
| 标准化 | POSIX通过O_NONBLOCK文件状态标志来描述套接字的I/O模式；非阻塞的connect()在连接已发起但尚未完成时返回[EINPROGRESS]<sup>[5](https://pubs.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_10.html)</sup> |
| TCP相关选项 | 内核TCP实现暴露套接字选项，如SO_SNDLOWAT、SO_RCVLOWAT和TCP_NOTSENT_LOWAT<sup>[6](https://www.rfc-editor.org/rfc/rfc9293.html)</sup> |

## 在伯克利的起源

该接口源于20世纪80年代初加州大学伯克利分校（University of California, Berkeley）的工作。技术报告《关于增强Vax上Unix的建议》（CSRG TR/4，1981年）很可能包含了后来成为BSD套接字接口的初稿；据马歇尔·柯克·麦库西克（Marshall Kirk McKusick）所述，那份讨论文稿演变成了4.2BSD系统手册（TR/5）。<sup>[4](https://elists.isoc.org/pipermail/internet-history/2017-November/004447.html)</sup> 在4.2BSD的实现中，对TCP/IP的访问通过套接字接口进行，当时的一份教程将套接字设施描述为一种进程间通信机制，使得分布式应用可以在网络上的Unix主机之间开发。<sup>[3](https://www.researchgate.net/publication/2806930_Packet-oriented_Communication_Using_a_Stream_Protocol_or_Making_TCPIP_on_Berkeley_UNIX_a_little_more_pleasant_to_use)</sup><sup> • </sup><sup>[7](https://www.sciencedirect.com/science/article/pii/0140366487903112/pdf?md5=a13660a96aef1b789b6ec3fe9807e24f&pid=1-s2.0-0140366487903112-main.pdf)</sup>

## 编程模型

针对该API编写的网络应用往往成对出现，分别对应协议的两端，如客户端与服务端，或发话方与监听方。<sup>[1](https://people.eecs.berkeley.edu/~culler/WEI/labs/lab5-sockets/sockets-intro.html)</sup> 调用也相应划分。服务端用socket()创建套接字，用bind()绑定本地地址，用listen()宣告愿意接受连接，并用accept()接收每个传入连接——accept()返回一个新套接字，可用于该连接上的send和recv。客户端调用connect()到达服务端地址，然后交换数据。<sup>[2](https://cs.brown.edu/people/slewando/files/IPCWinNTUNIX.pdf)</sup><sup> • </sup><sup>[8](https://docs.python.org/3/library/socket.html)</sup>

同一调用序列同时服务于IPv4和IPv6；例如，一个UDP客户端在IPv4下以环回地址127.0.0.1为目标，在IPv6下以::1为目标，再加上端口号。<sup>[1](https://people.eecs.berkeley.edu/~culler/WEI/labs/lab5-sockets/sockets-intro.html)</sup> 语言运行时直接暴露该接口：Python的socket模块函数——除makefile()外——与适用于套接字的Unix系统调用相对应。<sup>[8](https://docs.python.org/3/library/socket.html)</sup>

**阻塞与非阻塞操作。** 套接字的I/O模式由其打开文件描述上的O_NONBLOCK文件状态标志来描述。当设置了O_NONBLOCK时，connect()不阻塞即返回，并报告[EINPROGRESS]，表示连接已成功发起但尚未完成。<sup>[5](https://pubs.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_10.html)</sup>

## 套接字选项

该API包含一种通过命名选项来调整协议行为的机制。在TCP实现中，一些操作系统内核提供选项，用于指定套接字层将已发送数据传递给TCP之前（SO_SNDLOWAT）或将已接收数据传递给应用之前（SO_RCVLOWAT）缓冲的字节数。另一个选项TCP_NOTSENT_LOWAT限制写队列中未发送字节的数量，这有助于发送方应用避免构建大缓冲区及其带来的延迟；这对于在一条连接上复用交互式或实时流与批量传输的应用很有用。<sup>[6](https://www.rfc-editor.org/rfc/rfc9293.html)</sup> 应用还可以通过套接字选项禁用Nagle算法，这是许多应用使用的一项能力。<sup>[6](https://www.rfc-editor.org/rfc/rfc9293.html)</sup>

## BSD之外的移植性

该接口传播到其他操作系统，很大程度上是通过有意的兼容性工作实现的。Windows套接字的设计目的是便于伯克利套接字代码的轻松移植：WinSock v2.2 API支持伯克利API（有少量例外）并增加了Win32特有功能，微软为将伯克利（BSD）UNIX套接字应用移植到Winsock的开发者记录了差异。<sup>[2](https://cs.brown.edu/people/slewando/files/IPCWinNTUNIX.pdf)</sup><sup> • </sup><sup>[9](https://learn.microsoft.com/ms-my/windows/win32/winsock/porting-socket-applications-to-winsock)</sup> 针对Windows移植的实用指南包括：在任何套接字调用之前于WSAStartup中请求2.2版本，并使用getaddrinfo而非较旧的gethostbyname。<sup>[10](https://learn.microsoft.com/en-us/windows/win32/winsock/getting-started-with-winsock)</sup>

## 性能与更新的接口

BSD套接字API在高性能环境中带来可测量的开销。加州大学伯克利分校一项关于该API低效之处的研究测得，八核上的套接字分配开销为单核成本的4.2倍。<sup>[11](https://escholarship.org/content/qt5qv03232/qt5qv03232.pdf?t=q0naxb)</sup> io_uring方面的工作在内核层面持续推进，包括零拷贝接收支持以及对通过io_uring命令发出的套接字选项操作的修复。<sup>[12](https://lists.openwall.net/netdev/2025/02/12/332)</sup><sup> • </sup><sup>[13](https://lists.openwall.net/linux-kernel/2026/02/28/1702)</sup>

## References

1. [David E. Culler, "OS Networking APIs – Implementing Clients and Servers," Lab 5, Wireless Embedded Internetworking Short Course, UC Berkeley](https://people.eecs.berkeley.edu/~culler/WEI/labs/lab5-sockets/sockets-intro.html)
2. ["Interprocess Communication in Windows NT and UNIX," Brown University (WinSock v2.2 and Berkeley API)](https://cs.brown.edu/people/slewando/files/IPCWinNTUNIX.pdf)
3. ["Packet-oriented Communication Using a Stream Protocol, or Making TCP/IP on Berkeley UNIX a little more pleasant to use"](https://www.researchgate.net/publication/2806930_Packet-oriented_Communication_Using_a_Stream_Protocol_or_Making_TCPIP_on_Berkeley_UNIX_a_little_more_pleasant_to_use)
4. [Paul Ruizendaal, "Early sockets discussion paper," internet-history list, November 2017](https://elists.isoc.org/pipermail/internet-history/2017-November/004447.html)
5. [The Open Group, System Interfaces Chapter 2 (socket I/O modes, connect)](https://pubs.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_10.html)
6. [RFC 9293, Transmission Control Protocol (TCP)](https://www.rfc-editor.org/rfc/rfc9293.html)
7. ["Tutorial guide to Unix sockets for network communications," Computer Communications (ScienceDirect)](https://www.sciencedirect.com/science/article/pii/0140366487903112/pdf?md5=a13660a96aef1b789b6ec3fe9807e24f&pid=1-s2.0-0140366487903112-main.pdf)
8. [Python 3 documentation, socket — Low-level networking interface](https://docs.python.org/3/library/socket.html)
9. [Microsoft Learn, "Porting Socket Applications to Winsock"](https://learn.microsoft.com/ms-my/windows/win32/winsock/porting-socket-applications-to-winsock)
10. [Microsoft Learn, "Getting started with Winsock"](https://learn.microsoft.com/en-us/windows/win32/winsock/getting-started-with-winsock)
11. [UC Berkeley eScholarship, study of inefficiency in the BSD socket API](https://escholarship.org/content/qt5qv03232/qt5qv03232.pdf?t=q0naxb)
12. [netdev mailing list, "io_uring zero copy rx, PATCH v13"](https://lists.openwall.net/netdev/2025/02/12/332)
13. [linux-kernel mailing list, io_uring/uring_cmd ioctl fix](https://lists.openwall.net/linux-kernel/2026/02/28/1702)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Networking fundamentals and architecture › Internet protocol suite

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/berkeley-sockets

English original: https://www.edgepedia.org/berkeley-sockets
