简体   繁体   English

TCP/IP 发送数据和接收数据序列

[英]TCP/IP send data and receive data sequence

I am using uIP Open Source TCP Stack on my Embedded Microcontroller.我在嵌入式微控制器上使用 uIP 开源 TCP 堆栈。 I have read many post of TCP/IP which looks similar to this question but still I couldn't get the answer I was looking for.我已经阅读了许多看起来类似于这个问题的 TCP/IP 帖子,但我仍然无法得到我正在寻找的答案。 Thus I am asking this.因此我问这个。

I know send data will be saved in Ethernet Controller(Server) Tx Buffer in sequence.我知道发送数据将按顺序保存在以太网控制器(服务器)Tx 缓冲区中。 And Receive data will be saved in Ethernet Controller(Client) Rx Buffer in sequence.并且接收数据将依次保存在以太网控制器(客户端)Rx 缓冲区中。

If I have 100 bytes of message which I send (using PSOCK_SEND method ) all together to client using TCP.如果我有 100 字节的消息(使用 PSOCK_SEND 方法)一起发送到使用 TCP 的客户端。 I know if I send all together receiver will guarantee receive all together.我知道如果我一起发送接收器将保证一起接收。

Now If I send two 50 bytes one by one, it there any possibility that second 50 bytes sent can be received first.现在,如果我一个一个地发送两个 50 字节,则有可能首先接收到第二个发送的 50 个字节。 And first 50 bytes sent can be received second?发送的前 50 个字节可以第二次接收吗?

My understanding is that this is possible in HTTP Protocol.我的理解是这在 HTTP 协议中是可能的。 But in TCP regardless of bytes sent in two steps or one, the receive will receive in the same sequence.但是在 TCP 中,无论字节以两步还是一步发送,接收都会以相同的顺序接收。 So TCP will keep sending first 50 bytes until it will be successful or time out.所以 TCP 将继续发送前 50 个字节,直到它成功或超时。 And only then it will send second 50 bytes.只有这样它才会发送第二个 50 字节。

I know if I send all together receiver will guarantee receive all together.我知道如果我一起发送接收器将保证一起接收。

The above is not guaranteed.以上不保证。 TCP is a byte-stream oriented protocol, not a message-oriented protocol, so it does not guarantee anything other than that the data bytes will be received in the same order that they were sent. TCP 是一个面向字节流的协议,而不是一个面向消息的协议,因此它不保证除了数据字节将按照与发送相同的顺序接收之外的任何其他内容。 The receiver might receive all 100 bytes via a single recv() call, or it might (at least in principle) receive 1 byte at a time across 100 recv() calls, or anywhere in between, and it is up to the receiving code to be prepared to handle the incoming data correctly regardless of what the sizes are of the chunks that it arrives in.接收器可能通过单个 recv() 调用接收所有 100 个字节,或者它可能(至少原则上)在 100 个 recv() 调用中一次接收 1 个字节,或者介于两者之间,这取决于接收代码准备好正确处理传入的数据,而不管它到达的块的大小是多少。

Now If I send two 50 bytes one by one, it there any possibility that second 50 bytes sent can be received first.现在,如果我一个一个地发送两个 50 字节,则有可能首先接收到第二个发送的 50 个字节。 And first 50 bytes sent can be received second?发送的前 50 个字节可以第二次接收吗?

As far as the application is concerned, no.就申请而言,没有。 TCP guarantees in-order reception of the TCP data at the application level. TCP 保证在应用层按顺序接收 TCP 数据。 (The underlying packets might be received out of order by the TCP stack, but the TCP stack won't present them to the application layer out of order. Rather, it will wait until it has some more data ready in the correct order before causing recv() to return with more data) (底层数据包可能会被 TCP 堆栈乱序接收,但 TCP 堆栈不会乱序将它们呈现给应用层。相反,它会等待,直到它以正确的顺序准备好更多数据,然后再导致recv() 返回更多数据)

My understanding is that this is possible in HTTP Protocol.我的理解是这在 HTTP 协议中是可能的。

Since HTTP is built on top of TCP, any guarantees that TCP makes will also be true for HTTP.由于 HTTP 建立在 TCP 之上,因此 TCP 做出的任何保证也适用于 HTTP。 In particular, HTTP data will be received in the order in which the sender sent it.特别是,HTTP 数据将按照发送方发送的顺序接收。 (You might be a little confused by something in the HTTP spec that says eg that the sender can send HTTP headers in different orders, but note that whatever order the sender sends them in will nevertheless be the exact same order that the receiver receives them in, since they are all going over the TCP layer's byte-stream which is strictly FIFO) (您可能对 HTTP 规范中的某些内容感到有些困惑,例如,发送方可以以不同的顺序发送 HTTP 标头,但请注意,无论发送方发送它们的顺序如何,都将与接收方接收它们的顺序完全相同,因为它们都经过 TCP 层的字节流,这是严格的 FIFO)

So TCP will keep sending first 50 bytes until it will be successful or time out.所以 TCP 将继续发送前 50 个字节,直到它成功或超时。 And only then it will send second 50 bytes.只有这样它才会发送第二个 50 字节。

Sorry, but your understanding of how TCP works is not correct.抱歉,您对 TCP 工作原理的理解不正确。 TCP has a window size which is the number of bytes that can be sent on a connection without waiting for an acknowledgement from the receiver. TCP 有一个窗口大小,它是在不等待接收方确认的情况下可以在连接上发送的字节数。

So for example, if you send two 50 byte packets, and the TCP window is at least 100 bytes, then the packets can both be sent without waiting for a response.因此,例如,如果您发送两个 50 字节的数据包,并且 TCP 窗口至少为 100 字节,则无需等待响应即可发送这两个数据包。 And depending on network conditions, the packets may arrive at the receiver in reverse order.并且根据网络状况,数据包可能以相反的顺序到达接收器。 Therefore, the packets that you read from the Ethernet Controller(Client) Rx Buffer are not necessarily in order.因此,您从以太网控制器(客户端)读取数据包Rx缓冲区不一定是为了。

It's up to the TCP Stack software to re-order the packets as needed, before passing the packet payloads to the higher level protocol (which in your case is HTTP).在将数据包有效负载传递给更高级别的协议(在您的情况下是 HTTP)之前,由 TCP 堆栈软件根据需要重新排序数据包。 Because the TCP Stack software reorders the packets, the HTTP software will always see the bytes in the same order that they were sent.由于 TCP 堆栈软件对数据包重新排序,因此 HTTP 软件将始终按照发送字节的顺序查看字节。


In response to the comment:回应评论:

I have got two contrasting answers which I want to clarify.我有两个截然不同的答案,我想澄清一下。 Is this means, for application layer, the bytes will be in order regardless of TCP Window Size?这是否意味着,对于应用程序层,无论 TCP 窗口大小如何,字节都会按顺序排列? (1) As far as the application is concerned, TCP guarantees in-order reception of the TCP data at the application level. (1) 就应用而言,TCP保证在应用层面对TCP数据的有序接收。 (2) TCP has a window size: if you send two 50 byte packets, and the TCP window is at least 100 bytes, then depending on network conditions, the packets may arrive at the receiver in reverse order. (2) TCP 有窗口大小:如果发送两个 50 字节的数据包,并且 TCP 窗口至少为 100 字节,那么根据网络情况,数据包可能会以相反的顺序到达接收方。

在此处输入图片说明

Statement (1) applies to the interface between the TCP layer and the application layer.语句(1)适用于TCP层和应用层之间的接口。 Statement (2) applies to the interface between the hardware and the TCP layer.语句(2)适用于硬件和 TCP 层之间的接口。 There is no contradiction in the two statements because the TCP layer re-orders packets as necessary.这两个语句没有矛盾,因为 TCP 层会根据需要对数据包进行重新排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM