TCP 是在每个数据包上发送 SYN/ACK 还是仅在第一个连接上发送? [英] Does TCP send a SYN/ACK on every packet or only on the first connection?

查看:24
本文介绍了TCP 是在每个数据包上发送 SYN/ACK 还是仅在第一个连接上发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TCP 服务器,它监听传入的客户端,然后每秒向它发送一个数据包.我想知道,SYN/ACK 数据包是否只在初始连接时发送,所以它看起来像这样:

I have a TCP server that listens for an incoming client, then sends it one packet of data every second. I was wondering, does the SYN/ACK packet only get sent on initial connection, so it looks like this:

<client connect>
SYN
ACK
DATA
DATA
DATA
<client disconnect>

还是像这样随每个数据包一起发送?

Or does it get sent with every packet, like this?

<client connect>
SYN
ACK
DATA

SYN
ACK
DATA

SYN
ACK
DATA
<client disconnect>

另外,如果是第一种情况,如果你只是长时间保持连接打开,UDP 优于 TCP 有什么好处吗?

Also, if it's the first case, are there any benefits of UDP over TCP if you just keep the connection open over a long period of time?

推荐答案

有点像:

+-------------------------------------------------------+
|     client           network            server        |
+-----------------+                +--------------------|
|    (connect)    | ---- SYN ----> |                    |
|                 | <-- SYN,ACK -- |     (accepted)     |
|   (connected)   | ---- ACK ----> |                    |
\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/

when client sends...
\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/
|                 |                |                    |
|     (send)      | ---- data ---> |                    |
|                 | <---- ACK ---- |  (data received)   |
\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/

when server sends...
\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/
|                 |                |                    |
|                 | <--- data ---- |       (send)       |
| (data received) | ---- ACK ----> |                    |
\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/

...and so on, til the connection is shut down or reset

SYN 开始一个连接;您通常只会在建立连接时看到它.但是所有通过 TCP 发送的数据都需要一个 ACK​​.发送的每个字节都必须考虑,否则将被重新传输(或连接重置(关闭),在严重的情况下).

SYN starts a connection; you'll usually only see it when the connection's being established. But all data being sent via TCP requires an ACK. Every byte sent must be accounted for, or it will be retransmitted (or the connection reset (closed), in severe cases).

实际的连接通常与上图完全不一样,但原因有两个:

Actual connections aren't usually exactly like the diagram above, though, for two reasons:

  • ACK 可以累积,因此一个 ACK​​ 可以确认在此之前收到的所有内容.这意味着您可以使用一个 ACK​​ 确认两个或多个发送.
  • ACK 只是 TCP 标头中的一个标志和字段.发送一个至少需要一个标头的带宽,再加上较低层附加的任何内容.但是数据段已经包含了所有这些......所以如果你正在发送数据,你可以同时发送一个 ACK​​ 免费.

大多数 TCP/IP 堆栈都试图减少裸 ACK 的数量,而不会过度冒险重新传输或重置连接.所以像这样的对话是很有可能的:

Most TCP/IP stacks try to reduce the number of naked ACKs without unduly risking retransmission or a connection reset. So a conversation like this one is quite possible:

\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/
|                 |                |                    |
|                 | <--- data ---- |       (send)       |
| (data received) |                |                    |
|     (send)      | -- data,ACK -> |                    |
|                 |                |  (data received)   |
|                 | <- data,ACK -- |       (send)       |
| (data received) |                |                    |
|  (wait a bit)   | <--- data ---- |       (send)       |
| (data received) |                |                    |
|     (send)      | -- data,ACK -> |                    |
|                 |                |  (data received)   |
|     (send)      | ---- data ---> |   (wait a bit)     |
|                 |                |  (data received)   |
|                 | <- data,ACK -- |       (send)       |
| (data received) |                |                    |
|  (wait a bit)   |   (dead air)   |                    |
|                 | ---- ACK ----> |                    |
\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/\_/

<小时>

对于 UDP,没有内置的 SYN 和 ACK 概念 - UDP 本质上是不可靠的",并且不是面向连接的,因此这些概念并不适用.您的确认通常只是服务器的响应.但是一些建立在 UDP 之上的应用层协议会有一些特定于协议的方式来确认发送和接收的数据.


As for UDP, there's no built-in concept of SYN and ACK -- UDP is by nature "unreliable", and not connection-oriented, so the concepts don't apply as much. Your acknowledgement will usually just be the server's response. But some application-layer protocols built on top of UDP will have some protocol-specific way of acknowledging data sent and received.

这篇关于TCP 是在每个数据包上发送 SYN/ACK 还是仅在第一个连接上发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆