使用 Flash/Adobe Air 2.0 套接字时遇到困难
我正在尝试将一些数据写入原始数据套接字(大约 22 MB)。场景是这样的:-
- 打开本地文件
- 读取一大块字节。
- 将其写入套接字
- 重复 2 & 3 直到文件末尾。
现在的问题是我的代码(如下)没有传输完整的文件。它可以通过我的测试文件传输 22 MB 中的 3 个。然而,跟踪是完整的,并且显示正在传输的完整数据。我怀疑它可能在完成当前块之前开始写入下一个块(尽管我不确定)。
while(fs.bytesAvailable > 0){
var readAmount = (fs.bytesAvailable < socketBufferSize) ? fs.bytesAvailable : socketBufferSize;
seq++;
air.trace(">"+seq+" WritePacket "+readAmount+" "+fs.position+" "+fs.bytesAvailable);
fs.readBytes(bytes, 0, readAmount);
air.trace(bytes.length);
socket2.writeBytes(bytes, 0, bytes.length);
socket2.flush();
}
fs.close();
socket2.close();
上面是我应该使用的代码。我想知道我做的事情是否正确/错误。
在写入迭代之间插入强制延迟可确保文件完全传输,如下面的代码片段所示。然而,这不是一个可接受的解决方案。我想知道是否有一些我应该订阅的活动,或者是否需要采取不同的措施。另一端的服务器是FileZilla FTP Server。
var sendData = function (){
if(fs.bytesAvailable > 0){
var readAmount = (fs.bytesAvailable < socketBufferSize) ? fs.bytesAvailable : socketBufferSize;
seq++;
air.trace(">"+seq+" WritePacket "+readAmount+" "+fs.position+" "+fs.bytesAvailable);
fs.readBytes(bytes, 0, readAmount);
air.trace(bytes.length);
socket2.writeBytes(bytes, 0, bytes.length);
socket2.flush();
}
else{
air.trace("Closing Connection");
fs.close();
socket2.close();
}
}
var interval = setInterval(sendData, 100);
谢谢
I'm trying to write some data to a raw data socket(around 22 MB's). The scenario is such:-
- Open local file
- Read a chunk of bytes.
- Write it to the Socket
- Repeat 2 & 3 until the end of the file.
Now the problem is that my code(Below) is not transferring the complete file. It transfers maybe 3 out the the 22 MB with my test file. The trace however is complete and shows complete data being transmitted. I suspect that maybe it starts writing the next chunk before finishing the current one(though I'm not sure).
while(fs.bytesAvailable > 0){
var readAmount = (fs.bytesAvailable < socketBufferSize) ? fs.bytesAvailable : socketBufferSize;
seq++;
air.trace(">"+seq+" WritePacket "+readAmount+" "+fs.position+" "+fs.bytesAvailable);
fs.readBytes(bytes, 0, readAmount);
air.trace(bytes.length);
socket2.writeBytes(bytes, 0, bytes.length);
socket2.flush();
}
fs.close();
socket2.close();
Above is the code that I'm supposed to be using. I would like to know if I'm doing anything right/wrong.
Inserting a forced delay between write iterations ensures that the file gets completely transferred indicated in the snippet below. However this is not an acceptable solution. I would like to know if there is some event I should be subscribing to or if anything needs to be done differently. The server on the other end is FileZilla FTP Server.
var sendData = function (){
if(fs.bytesAvailable > 0){
var readAmount = (fs.bytesAvailable < socketBufferSize) ? fs.bytesAvailable : socketBufferSize;
seq++;
air.trace(">"+seq+" WritePacket "+readAmount+" "+fs.position+" "+fs.bytesAvailable);
fs.readBytes(bytes, 0, readAmount);
air.trace(bytes.length);
socket2.writeBytes(bytes, 0, bytes.length);
socket2.flush();
}
else{
air.trace("Closing Connection");
fs.close();
socket2.close();
}
}
var interval = setInterval(sendData, 100);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于AIR具有残酷的套接字关闭操作,因此需要修改服务器以让客户端知道它何时收到了所有发送的数据(通过另一个TCP控制套接字)。当然,客户端需要在开始时中继其发送的数据量。
Since AIR has a brutal socket close operation, the server needs to be modified to let the client know when it has received all the data sent(via another TCP control Socket). Of course the client needs to relay how much data it is sending at the start.
我已经有一段时间等待数据完全写入时触发的套接字事件了...:(
第一个想法是在每次 write() 之后放置一个延迟。但是,如果连接确实良好,则传输是由于延迟而较长。并且在连接速度较慢的情况下,延迟并不总是足够,因此仍然可能会丢失一些数据:(
我发现的唯一一种解决方案并不真正合适,但有效。我正在使用 proftpd作为 FTP 服务器,带有我编写的 MOD,允许新的 FTP 命令:FILESIZE。
此命令给出文件路径的大小(以八位字节为单位)。
然后,在flex中,我打开了2个套接字:一个用于传输数据,一个用于获取文件大小。
在第一个套接字上每次调用 write() 后,我都会在第二个套接字上使用 FILESIZE 获取文件的大小。
另一方面,由于 FILESIZE (:/) 的 FTP 调用,传输需要很长时间,但传输更可靠。
这种问题可能会从 Flex 转移到 Java。近两年来,这个问题一直发布在 Adobe Bug 页面上,但……什么也没有……
有没有办法在 flex 的核心中添加这个功能?我不这么认为,flash不是开源的......
It's been a while that i'm waiting for a socket event triggered when data is fully written... :(
The first idea is to put a delay after each write(). However, in case of really good connection, the transfer is longer because of the delay. And in case of slow connection, the delay is not always sufficient and so some data can still be lost :(
The only one solution that i've found is not really proper but works. I'm using proftpd as FTP server with a MOD that i've written that allows a new FTP command : FILESIZE.
This command give the size of a filepath in octets.
Then, in flex, i've 2 sockets opened : one for transmitting the data, one to get the filesize.
After each call of write() on the first socket, i get the size of the file using FILESIZE on the second socket.
The other side of the coin is that the transfer take much time because of the FTP calls of FILESIZE (:/) but the transfer is more reliable.
This is the kind of problem that will probably move from Flex to Java. Alomost two years that is problem is posted on Adobe Bug pages, and... nothing...
Is there a way to add this feature in the core of flex ? I don't think so, flash is not open source...