Tcp连接中可以分离一些数据吗? [英] Can some data be separated in a Tcp connection?
问题描述
我的问题很简单.可以在 tcp 连接中分离一些数据吗(如标题所说)?例如,如果我调用 MySocket.Send(Encoding.UTF8.GetBytes("this is a test!");
,我可以从连接的另一端?即Tcp有多可靠?
my question is simple. Can some data be separated in a tcp connection (as title says)? For example if I call MySocket.Send(Encoding.UTF8.GetBytes("this is a test!");
, may I get 2 separate receive operations like "this is" and " a test" from the other side of connection? Namely how much reliable is the Tcp?
推荐答案
TCP 通常用作流;不保证无论如何发送"和接收"操作将一对一匹配.必须读取 15 次,每次获取 1 个字节,这在技术上是有效的(但不太可能).因此,通常有两种操作模式:
TCP is usually used as a stream; no guarantee whatsoever is made that the "send" and "receive" operations will match up one-to-one. It would be technically valid (but unlikley) to have to read 15 times, getting 1 byte each time. As such, there are usually two modes of operation:
- 读取直到流结束(发送者在发送消息后关闭其出站套接字)
- 添加某种帧协议
- 对于基于文本的协议,这通常是一个标记值,例如
0
或行尾(是 CR、LF、CRLF 还是 LFCR 取决于您!) - 对于二进制协议,这通常是一些定义明确的格式的长度前缀,告诉您下一条消息要考虑多少字节
- read until the end of the stream (the sender closes their outbound socket after sending the message)
- add some kind of framing protocol
- for text-based protocols this is often a sentinel value, such as a
0
or an end-of-line (whether that is CR, LF, CRLF or LFCR is up to you!) - for binary protocols, this is often a length-prefix in some well-defined format, telling you how many bytes to consider for the next message
(但这条线很模糊;例如,redis 协议大致基于文本,但使用了
${n}\r\n{data}\ 形式的纯文本长度前缀r\n
,其中{n}
是表示为 ASCII 字符串的字节数,{data}
是原始的n代码>字节)
(but the line is blurry; the redis protocol, for example, is roughly text-based, but uses a plain-text length-prefix of the form
${n}\r\n{data}\r\n
, where{n}
is the number of bytes expressed as an ASCII string, and{data}
is the rawn
bytes)这篇关于Tcp连接中可以分离一些数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- for text-based protocols this is often a sentinel value, such as a
- 对于基于文本的协议,这通常是一个标记值,例如