从一个插座所接收的流不限于单个发送命令? [英] Is the received stream from a socket limited to a single send command?

查看:169
本文介绍了从一个插座所接收的流不限于单个发送命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的工作在哪里,我收到使用以下(简化)code数据多线程应用程序:

I currently work on a multithreaded application where I receive data using the following (simplified) code:

private void BeginReceiveCallback(IAsyncResult ar)
{
bytesReceived = this.Socket.EndReceive(ar);
byte[] receivedData = new byte[bytesReceived];
Array.Copy(buffer, receivedData, bytesReceived);

long protocolLength = BitConverter.ToInt64(receivedData, 0);
string protocol = Encoding.ASCII.GetString(receivedData, 8, (int)protocolLength);
IList<object> sentObjects = 
ParseObjectsFromNetworkStream(receivedData, 8 + protocolLength);

InvokeDataReceived(protocol, sentObjects);
}

我遇到了 receivedData 不仅包含预期的数据,而且还多了很多。我怀疑这是事后的数据发送已经混在previous的激流。

I'm experiencing that receivedData contains not only the expected data, but also a lot more. I suspect that this is data sent afterwards that has been mixed in with the previous in the stream.

我的问题是,什么样的数据我可以期望能够存储的缓冲。它可以从客户端包含来自两个不同发送操作数据?在这种情况下,我想我将不得不拿出一个协议,可以将数据消息从客户端发送区分。一种简单的方法是将分别开始和结束的每个流与特定的(唯一的)字节。是否有一个共同的方式来分隔条件的邮件?此外,我想这意味着单个接收调用可能不足以获得来自客户端,这意味着我将不得不循环,直到最后一个字节被发现?所有的数据。

My question is, what data can I expect to be stored in this buffer. Can it contain data from two different send operations from the client side? In this case, then I suppose I will have to come up with a protocol that can differentiate between the data 'messages' sent from the client side. A simple approach would be to respectively start and end each stream with a specific (unique) byte. Is there a common approach to seperating messages? Furthermore I guess this means that a single receive call might not be enough to get all the data from the client which means I'll have to loop until the end byte was found?

推荐答案

TCP / IP套接字连接由两个独立的数据流:一个输入和一个输出

TCP/IP socket connections consist of two independent streams: one incoming and one outgoing.

这是TCP / IP的,往往是错过了关键概念之一。从应用的角度看,则TCP / IP没有报文操作;它运行在流!

This is one of the key concepts of TCP/IP that is often missed. From the perspective of the application, TCP/IP does not operate on packets; it operates on streams!

没有方法发送的分组。该API根本不存在。当您发送数据时,您只需将这些字节传出激流。他们然后从在另一侧进入的流读

There is no method to send a packet. The API simply does not exist. When you send data, you just place those bytes in the outgoing stream. They are then read from the incoming stream on the other side.

作为一个例子,一边可以发送5个字节,然后发送另一个5个字节。接收侧可以在一个单一的读出接收的两批5个字节,或一次一个,或所有10 ...

As an example, one side can send 5 bytes and then send another 5 bytes. The receiving side can receive two batches of 5 bytes, or one at a time, or all 10 in a single read...

要字节输入流分割成信息,你需要的信息帧。之一的两个解决方案是常用。一个你认为是在分隔符解决方案,其中SOT / EOT字节用于指定消息边界。另外一个(我preFER)是在长度preFIX 解决方案,其中消息的长度pfixed邮件本身$ P $。

To split the incoming stream of bytes into messages, you need message framing. One of two solutions is commonly used. The one you suggested is the delimiter solution, where SOT/EOT bytes are used to designate message boundaries. Another one (which I prefer) is the length prefix solution, where the length of the message is prefixed to the message itself.

有一个更深入的讨论是在我的博客,随着< A HREF =htt​​p://nitoprograms.blogspot.com/2009/04/sample-$c$c-length-$p$pfix-message.html相对=nofollow>样本code的长度prefixing 。

A more thorough discussion is on my blog, along with sample code for length prefixing.

这篇关于从一个插座所接收的流不限于单个发送命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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