> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/4.3-bsd-unix-cao-zuo-xi-tong-she-ji-yu-shi-xian/di-si-bu-fen-jin-cheng-jian-tong-xin/10.2.-shi-xian-jie-gou-yu-gai-shu.md).

# 10.2 实现结构与概述

如图 10.2 所示，进程间通信设施分层叠加在网络设施之上。数据从应用经套接字层流向网络支持，反之亦然。套接字层所需的状态完全封装在套接字层内，而协议相关的状态维护在支撑协议专有的辅助数据结构中。传输数据所用存储的责任由套接字层移交网络层；始终如一地遵守这条规则有助于简化存储管理的细节。套接字层内，套接字数据结构是一切活动的焦点。系统调用接口例程管理与一次 UNIX 系统调用相关的动作，收集系统调用参数（见 3.2 节），把用户数据转换成第二级例程期望的格式。套接字抽象的大部分在第二级例程中实现。所有第二级例程的名字都带 `so` 前缀，直接操纵套接字数据结构并管理异步活动之间的同步；这些例程列于表 10.1。

![图 10.2 进程间通信实现的分层](https://1991889543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FilVST9xTgD47M0g8ham7%2Fuploads%2Fgit-blob-f8649a862ea8d80bdbecfc286fb17b0e65fcc157%2Ffig-10-2-figure-10-2-interprocess.png?alt=media)

表 10.1 套接字层支持例程

| 例程             | 功能            |
| -------------- | ------------- |
| `socreate()`   | 创建新套接字        |
| `sobind()`     | 把名字绑定到套接字     |
| `solisten()`   | 把套接字标记为监听连接请求 |
| `soaccept()`   | 接受套接字上的未决连接   |
| `soconnect()`  | 发起到另一套接字的连接   |
| `soconnect2()` | 在两个套接字之间创建连接  |
| `sosend()`     | 发送数据          |
| `soreceive()`  | 接收数据          |
| `soshutdown()` | 关闭数据发送或接收     |
| `sosetopt()`   | 设置套接字选项的值     |
| `sogetopt()`   | 读取套接字选项的值     |

本章余下部分集中于套接字层的实现。10.3 节讨论套接字层及以下网络子系统的内存管理；10.4 节讲套接字及相关数据结构；10.5 节给出连接建立的算法；10.6 节讨论数据传输；10.7 节描述连接关闭。本章中提到网络通信协议提供的支撑设施时一带而过，不作展开；网络协议与套接字层交互的完整描述见第 11 章，网络协议的内部在第 12 章呈现。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/4.3-bsd-unix-cao-zuo-xi-tong-she-ji-yu-shi-xian/di-si-bu-fen-jin-cheng-jian-tong-xin/10.2.-shi-xian-jie-gou-yu-gai-shu.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
