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

查看:334
本文介绍了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天全站免登陆