重组与闪存插槽拆分TCP包 [英] recombine split TCP packet with flash sockets

查看:200
本文介绍了重组与闪存插槽拆分TCP包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中发送的23723字节的插座Flash播放器于一体的枪毙的节目。

I have a program in c++ that sends a socket of 23723 bytes to a flash player in one "shot".

Flash播放器有时会受到大小17520和6203,以及其他时间的两包收到的23723单个数据包。

The flash player sometimes receives two packets of size 17520 and 6203, and other times it receives a single packet of 23723.

编辑:有似乎没有一种方法来获得与从闪存发送数据相关联的字节的总数。这将使它很难建立一个循环重建破的数据包。

There does not seem to be a way to obtain the total number of bytes associated with the send data from flash. This would make it very difficult to build a loop to reconstruct the "broken" packet.

我认为TCP应该正确地重新组合数据包,我有困难重组他们自己。

I thought TCP was supposed to correctly recombine the packets and I am having difficulty recombining them myself.

这是我的接收处理程序闪光灯:

This is my receiving handler in flash:

var socket:Socket = new Socket();
socket.addEventListener(Event.CONNECT, socketConnectHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataReceivedHandler);
socket.connect("127.0.0.1", 7445);
socket.writeUTFBytes("hi");

private function socketDataReceivedHandler(event:ProgressEvent):void {
  var socket:Socket = event.target as Socket;
  trace(socket.bytesAvailable);
  var data:ByteArray = new ByteArray();
  socket.readBytes(data);
}

命令:

trace(socket.bytesAvailable);

将在次印刷23723,并在其他时间,将打印17520紧接着6203.总规模只是一个例子,并有可能改变每次发送的插座。也有许多插座可以发送或在同一时间收到,所以拆分包可以相互混合。在C ++我能确定发送数据的总规模是,即使我没有收到全部发送的数据呢。

will at times print 23723, and at other times, will print 17520 quickly followed by 6203. The total size are just examples and are subject to change for each send sockets. Also many sockets can be sent or received at the same time, so the split packets can be inter-mixed. In c++ I could determine what the total size of the sent data was, even if I did not receive the whole sent data yet.

推荐答案

这已无关的TCP。插座可以只要发送任何数目的字节,因为它是至少一个字节。所以,你可能会得到[1,23722]。要做到这一点,正确的方法是调用读一个循环,并自己填写的数组。如果你的code不包含循环,但作品,那么它的工作原理是意外。这没有一个循环无法正常工作。

This has nothing to do with tcp. The socket can send you any number of bytes as long as it is at least one byte. So you might get [1, 23722]. The correct way to do this is to call read in a loop and fill an array yourself. If your code does not contain a loop but works, then it works by accident. This cannot work without a loop.

编辑:请不要使用方bytesAvailable。检查的读取功能的返回值。检查你的code:如果你的code依赖于信息bytesAvailable这是错误的。

do not use bytesAvailable. Check the return value of the read function. Check your code: If your code relies on bytesAvailable it is wrong.

EDIT2:看起来像闪光的API(我不知道)从所有其他框架和语言使用socket API的工作方式不同。我认为它的工作是相同的,所以让我正确的:你应该有下面的循环:

Looks like the flash api (which I do not know) works differently from the socket api that all other frameworks and languages use. I assumed it would work the same, so let me correct: You are supposed to have the following loop:

int writePos = 0;
var array = ...;
while true:
 wait for bytes to become available. i do not know how to do this but one method would be 'while bytesAvailable == 0 then sleep 10'
 var bytesAvailableCopy = bytesAvailable; //copy because this value can change anytime
 socket.read(array, writePos, bytesAvailableCopy );
 writePos += bytesAvailableCopy;
 if(writePos == maxLength) break;

这篇关于重组与闪存插槽拆分TCP包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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