Socket + amf3 package Sticking Problem

Source: Internet
Author: User
Tags uncompress

Problem:

The front-end of the Mina server receives the flex
The reference prototype is http://www.klstudio.com/post/202.html

<[Socket
+ Amf3] Try to develop flash socket and amf3
Web games
>
Individual Debugging Processes the length of messages sent and received by the server and client respectively
However, a large amount of data is sent on the server.
The client will still stick to the package, resulting in errors and frequent.
Obtain the sending and receiving code of the server and client
. To be practical.
(It is best to go through a certain stress test. Or press it on .)

I want to add a description of my progress tracking:
The client receives the following information:
The length is 34 bytes. Check that there are still 34 bytes in the socket.
However, an exception occurs when reading the object.
Flex
. Messaging. Io. unknowntypeexception: Unknown AMF type '000000'
120 is the first of the 34 bytes. There are other content such as-124.


 

Answer:

This is basically a classic problem for socket programming.
AMF itself is a binary encapsulation. When a packet bursts, the receiver may not be able to tell the beginning or end of the packet due to data accumulation, resulting in a ing error.
The solution is to simply package AMF before sending the package. Because AMF itself is a binary format, you only need to write the AMF length (that is, the body length) before it, the receiver, first read the length of the header, and then fix the body. After the body is retrieved, the headers are taken and the headers are obtained in sequence.
The structure of each sent package is

Struct
{
DWORD data_len // package Length
Char [..] // content
}


After the theory is complete, the key code is provided. I believe that the socket can be carefully studied at a Glance:

As3 ---> server sending

Public Function send (OBJ: Object)
{
VaR objbyte: bytearray = new bytearray ();
Objbyte. writeobject (OBJ );
// Objbyte. Compress (); // compression, which can be omitted
VaR msgbyte: bytearray = new bytearray ();
Msgbyte. writeint (objbyte. Length );
Msgbyte. writebytes (objbyte, 0, objbyte. Length );

SK. writebytes (msgbyte );
SK. Flush ();
}

Server --> as3 receiving part

// Read the data as soon as it receives it
Private function onskdata (E: progressevent): void
{
This. readdata ();
}
Private function readdata (): void
{
// If you have not read the header, read it once.
If (! This. isreadhead & this. sk. bytesavailable> 4)
{
VaR lenbyte: bytearray = new bytearray ();
SK. readbytes (lenbyte, 0, 4 );
This. msglen = lenbyte. readint ();
// Trace ("New message length:" + this. msglen );
This. isreadhead = true;
}
// If the read header is read and the current read length is greater than or equal to the message length, the read starts.
If (isreadhead & this. sk. bytesavailable> = This. msglen)
{
VaR objbyte: bytearray = new bytearray ();
SK. readbytes (objbyte, 0, this. msglen );
This. isreadhead = false;
// Objbyte. uncompress ();
VaR OBJ: Object = objbyte. readobject ();
This. readmsg (OBJ); // after reading, you can explain it.
}
// If the read header is used, if the current message length is greater than the message length, the system calls the read request again. Otherwise, the system determines whether the read header is readable.
This. sk. bytesavailable> 0 & this. readdata ();

}

As for the server side, the principle is the same. It reads the header first when reading, and also starts first when sending.

Supplement the server code (C #, the decoding class is fluorinefx, for reference only)

Server-> as3 send


Public void send (Object OBJ)
{
Try
{
Bytearray objbyte = new bytearray ();
Objbyte. writeobject (OBJ );
// Objbyte. Compress ();
Byte [] objbuff = new byte [objbyte. Length];
Objbyte. Position = 0;
Objbyte. readbytes (objbuff, (uint) 0, (uint) objbuff. Length );

Bytearray msgbyte = new bytearray ();
Msgbyte. writeint (objbuff. Length );
Msgbyte. writebytes (objbuff, 0, objbuff. Length );
Byte [] msgbuff = new byte [msgbyte. Length];
Msgbyte. Position = 0;
Msgbyte. readbytes (msgbuff, (uint) 0, (uint) msgbuff. Length );
// Send
This. Ng. sk. beginsend (msgbuff, 0, msgbuff. length, socketflags. None, onsendover, this. NG );

}
Catch
{
Core. mainform. addlog ("failed to send ");
}
}


    As3-> Server Accept


    // Receives the received callback.
    Private void receover (iasyncresult AR)
    {
    Netgate ng = (netgate) Ar. asyncstate;
    Int size = Ng. sk. endreceive (AR );
    // Core. mainform. addlog ("Total accepted length:" + size );
    Int ind = 0;
    If (size> 0)
    {
    While (size> 0)
    {
    // Read the message header
    Memorystream lenms = new memorystream (NG. Buff, IND, 4 );
    Bytearray lenbyte = new bytearray (lenms );
    Int32 Len = lenbyte. readint ();
    // Core. mainform. addlog ("message length:" + Len. tostring ());
    If (LEN = 0) {break ;}
    // Get content based on the header length
    Memorystream MS = new memorystream (NG. Buff, IND + 4, Len );
    Bytearray BA = new bytearray (MS );
    // Ba. uncompress ();
    Object OBJ = ba. readobject ();
    This. readmsg (OBJ );
    IND + = (LEN + 4 );
    Size-= (LEN + 4 );
    }
    This. Ng. Buff. initialize ();
    Ng. sk. beginreceive (this. Ng. Buff, 0, this. Ng. buffsize, socketflags. None, this. onreceover, this. NG );
    }
    Else
    {
    Ng. sk. Close ();
    This. Offline ();
    }
    }

    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.