AS3 TCP Socket 拆包
AS3 TCP Socket 拆包
- 作者:柳大·Poechant(鍾超)
- 郵箱:zhongchao.ustc#gmail.com(# -> @)
- 部落格:Blog.CSDN.net/Poechant
- 微博:weibo.com/lauginhom
- 日期:June 2nd, 2012
在 AS3 中的 Socket 只有 TCP 一種方式,而 TCP 就必然面臨拆包的問題。對於 Windows、Linux、Mac等作業系統的不同網路模型,Flash 程式在不拆包下的執行糟糕程度不一樣,我的親身體會是 Windows 的網路模型會積攢更大的緩衝區資料讓 Flash TCP Socket 每次收到的資料量都比 Mac 上的多,所以情況就更糟糕。這時候拆包的效果就十分明顯。
下面有一段更形象化的解釋,摘自 Netty 的
One Small Caveat of Socket Buffer
In a stream-based transport such as TCP/IP, received data is stored into a socket
receive buffer. Unfortunately, the buffer of a stream-based transport is not a queue of
packets but a queue of bytes. It means, even if you sent two messages as two independent
packets, an operating system will not treat them as two messages but as just a bunch of
bytes. Therefore, there is no guarantee that what you read is exactly what your remote
peer wrote. For example, let us assume that the TCP/IP stack of an operating system has
received three packets:
+-----+-----+-----+
| ABC | DEF | GHI |
+-----+-----+-----+
Because of this general property of a stream-based protocol, there's high chance of reading them in the following fragmented form in your application:
+----+-------+---+---+
| AB | CDEFG | H | I |
+----+-------+---+---+
Therefore, a receiving part, regardless it is server-side or client-side, should defrag the
received data into one or more meaningful frames that could be easily understood by the
application logic. In case of the example above, the received data should be framed like the
following:
+-----+-----+-----+
| ABC | DEF | GHI |
+-----+-----+-----+
下面例項中的 _socket 和 lastReservedLength 都是在函式外部定義的,其定義不影響程式的理解。各段含義我寫在了程式的註釋中。
部分程式碼如下:
private function onResponse(e:ProgressEvent):void
{
// Data available from socket
while (_socket.bytesAvailable > 0)
{
var tmpLength:int = 0;
// Last package received is incomplete, and package length maintains last reserved length
if (lastReservedLength > 0)
{
// CASE 1: Package is still incomplete after receiving data from socket at least once
if (lastReservedLength > _socket.bytesAvailable + 4) break;
// CASE 2: The last part of a package
else tmpLength = lastReservedLength;
}
// Last package received is complete, and package length should be read from socket
else
{
var lengthBytes:ByteArray = new ByteArray();
lengthBytes.endian = Endian.LITTLE_ENDIAN;
_socket.readBytes(lengthBytes, 0, 4); // TODO Crash!
//Error: Error #2030: End of file was encountered.
//at flash.net::Socket/readBytes()
tmpLength = lengthBytes.readInt();
// CASE 3: The first part of a package
if (_socket.bytesAvailable < tmpLength - 4)
{
lastReservedLength = tmpLength;
break;
}
}
// CASE 4: A complete package received once
// Generate ByteArray of a complete package
var resultBytes:ByteArray = new ByteArray();
resultBytes.writeInt(tmpLength);
_socket.readBytes(resultBytes, 4, tmpLength - 4);
lastReservedLength = 0;
// parse the ByteArray
resultBytes.position = 0;
var resProto:Object = CppLibUtil.parseMediaProto(resultBytes);
// Dispatch the ByteArray
if (resProto != null) {
var uri:String;
try {
uri = resProto.uri;
} catch (e:Event) {
trace(e);
}
dispatchEvent(new InternalEvent(uri, resProto));
}
}
}
-
轉載請註明來自柳大的CSDN部落格:Blog.CSDN.net/Poechant,微博:weibo.com/lauginhom
-
相關文章
- TCP 粘包拆包TCP
- TCP粘包拆包問題TCP
- TCP的粘包拆包技術TCP
- go語言處理TCP拆包/粘包GoTCP
- TCP 粘包 - 拆包問題及解決方案TCP
- 粘包/拆包問題一直都存在,只是到TCP就拆不動了。TCP
- 計算機網路 - TCP粘包、拆包以及解決方案計算機網路TCP
- Netty(三) 什麼是 TCP 拆、粘包?如何解決?NettyTCP
- HTTP、Socket與TCPHTTPTCP
- TCP/IP 和SocketTCP
- 深入學習Netty(5)——Netty是如何解決TCP粘包/拆包問題的?NettyTCP
- TCP socket和web socket的區別TCPWeb
- SOCKET通訊中TCP、UDP資料包大小的確定TCPUDP
- Netty - 粘包與拆包Netty
- socket,TCP/IP的理解TCP
- .NET TCP、UDP、Socket、WebSocketTCPUDPWeb
- socket與TCP/IP區別TCP
- Netty 中的粘包和拆包Netty
- 粘包拆包及解決方案
- TCP/IP、HTTP和Socket總結TCPHTTP
- day30:TCP&UDP:socketTCPUDP
- Linux學習/TCP Socket通訊LinuxTCP
- Linux Socket C語言網路程式設計:TCP SocketLinuxC語言程式設計TCP
- 《初識TCP》iOS、macOS實現socket client與socket serverTCPiOSMacclientServer
- Python:列表也能拆包?Python
- Netty如何解決粘包拆包?(二)Netty
- Socket和TCP連線過程解析TCP
- python-20-優化socket_tcpPython優化TCP
- PHP拆紅包演算法PHP演算法
- Netty拾遺(七)——粘包與拆包問題Netty
- 從linux原始碼看socket(tcp)的timeoutLinux原始碼TCP
- 從Linux原始碼看Socket(TCP)的bindLinux原始碼TCP
- 手把手教你 Socket 通訊(TCP/IP)TCP
- 從Linux原始碼看Socket(TCP)的acceptLinux原始碼TCP
- http、socket、tcp的區別和聯絡?HTTPTCP
- 網路基礎:TCP(3):TCP沾包TCP
- Python元組和字典的拆包Python
- socket程式設計在TCP中的應用程式設計TCP
- TCP、UDP、HTTP及Socket的簡單講解TCPUDPHTTP