# 7.TCP和UDP的区别

**这是传输层的两个协议，先说一下传输层的两大功能：**

1. 复用：在发送端，多个应用进程公用一个传输层；
2. 分用：在接收端，传输层会根据端口号将数据分给不同的应用进程。&#x20;

**传输层和网络层的区别：**

1. 网络层为不同的**主机**提供通信服务，传输层为不同应用**进程**提供通信服务。
2. 网络层只对报文头部进行差错检测，而传输层对整个报文进行差错检测。

**UDP（User Data Protocol）用户数据报协议**

1. 无连接
2. 不可靠（不能保证都送达）
3. 面向报文（UDP数据传输单位是报文，不会对数据进行拆分和拼接操作，只是给上层传来的数据加个UDP头或者给下层来的数据去掉UDP头）
4. 没有拥塞控制，始终以恒定速率发送数据
5. 支持一对一、一对多、多对多、多对一
6. 首部开销小，只有8字节

**TCP（Transmission Control Protocol）传输控制协议**

1. 有连接
2. 可靠的
3. 面向字节流
4. 全双工通信，TCP两端既可以作为发送端也可以作为接收端
5. 连接的两端只能是两个端点，即一对一，不能一对多
6. 至少20个字节，比UDP大的多

**什么是TCP连接**

TCP连接是一种抽象的概念，表示一条可以通信的链路。 每个TCP连接有且仅有两个端点，表示通信的双方，且双方在任意时刻都可以作为发送者和接受者。

**什么是套接字**

一条TCP连接的两端就是两个套接字。 套接字 = IP地址 ：端口号 因此，TCP连接 = （套接字1，套接字2）= （IP1：端口号1，IP2：端口号2）


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/interview/7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
