As3 TCP socket package removal

Source: Internet
Author: User
As3 TCP socket package removal
  • Author: Liu Da-poechant (Zhong Chao)
  • Email: zhongchao. USTC # gmail.com (#-> @)
  • Blog: blog.csdn.net/poechant
  • Weibo: weibo.com/lauginhom
  • Date: June 2Nd, 2012

In as3, the socket has only one TCP method, and TCP is bound to face the packet splitting problem. For different network models of windows, Linux, Mac, and other operating systems, flash programs run poorly without unpacking, my personal experience is that the windows network model will accumulate more buffer data so that the flash TCP socket receives more data each time than the Mac, so the situation is even worse. At this time, the effect of unpacking is very obvious.

The following is a more visualized explanation from netty's

One Small Caveat of Socket BufferIn a stream-based transport such as TCP/IP, received data is stored into a socketreceive buffer. Unfortunately, the buffer of a stream-based transport is not a queue ofpackets but a queue of bytes. It means, even if you sent two messages as two independentpackets, an operating system will not treat them as two messages but as just a bunch ofbytes. Therefore, there is no guarantee that what you read is exactly what your remotepeer wrote. For example, let us assume that the TCP/IP stack of an operating system hasreceived 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 thereceived data into one or more meaningful frames that could be easily understood by theapplication logic. In case of the example above, the received data should be framed like thefollowing:
+-----+-----+-----+| ABC | DEF | GHI |+-----+-----+-----+

In the following example, both _ socket and lastreservedlength are defined outside the function, and their definition does not affect the understanding of the program. I wrote the meanings of each section in the program's annotations.

Some code is as follows:

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));        }    }}

-

For more information, see the csdn blog blog.csdn.net/poechant,weibo.com/lauginhom.

-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.