在 TCP 中接收数据 [英] Receiving data in TCP

查看:25
本文介绍了在 TCP 中接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 TCP 中发送 1000 个字节,它是否保证接收者将一起"获得整个 1000 个字节?或者他可能首先只得到 500 个字节,然后他会收到其他字节?

If i send 1000 bytes in TCP, does it guarantee that the receiver will get the entire 1000 bytes "togther"? or perhaps he will first only get 500 bytes, and later he'll receive the other bytes?

EDIT:问题来自应用程序的观点.如果这 1000 个字节在到达应用程序之前被重新组合成一个缓冲区.. 那么我不在乎它是否被碎片化了..

EDIT: the question comes from the application's point of view. If the 1000 bytes are reassembles into a single buffer before they reach the application .. then i don't care if it was fragmented in the way..

推荐答案

参见 传输控制协议:

TCP 提供从一台计算机上的程序到另一台计算机上的另一个程序的可靠、有序的字节流传输.

TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer.

流"意味着从接收者的角度来看没有消息边界.您可能会收到一条 1000 字节的消息或一千条 1 字节的消息,具体取决于下面的内容以及您调用 read/select 的频率.

A "stream" means that there is no message boundary from the receiver's point of view. You could get one 1000 byte message or one thousand 1 byte messages depending on what's underneath and how often you call read/select.

编辑:让我从应用程序的角度澄清一下.不,TCP 不能保证单次读取会为您提供发送方可能发送的所有 1000 字节(或 1MB 或 1GB)数据包.因此,TCP 之上的协议通常包含固定长度的标头,其中包含总内容长度.例如,您可以始终发送 1 个字节,以字节为单位指示内容的总长度,最多可支持 255 个字节.

Edit: Let me clarify from the application's point of view. No, TCP will not guarantee that the single read would give you all of the 1000 bytes (or 1MB or 1GB) packet the sender may have sent. Thus, a protocol above the TCP usually contains fixed length header with the total content length in it. For example you could always send 1 byte that indicates the total length of the content in bytes, which would support up to 255 bytes.

这篇关于在 TCP 中接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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