Flash
Go to development
Application
And network communication, especially online games.
, Even if communication is required.
As3
Provides two classes that support Instant Messaging: XML
Socket
And socket, the former actually only supports sending strings, while the latter is much more powerful. It supports sending byte, which is relatively lower-layer, and gives us more space to use.
In fact, the underlying network of Flash socket receives data
In this example, the C ++ server sends a piece of data, each of which is composed of a certain structure, generally, it is the message header and message header, and then layer-by-layer parsing.
Here, I only want to accept the processing at the bottom layer of the network. Generally, there is a 4-byte data before a piece of data, it indicates the amount of data in the network stream ....
Easy to understand
Download
(21 KB)
Packet
What we need to do at the bottom layer of the network is to determine the data size next to the first four bytes, then extract the entire block and send it to the upper layer for resolution, and then listen continuously,
After receiving the specified size, perform similar operations. The detailed code is as follows:
/**
* @ (#) Netbottomhandle.
* @ Author soda. c mail: sujun10@21cn.com
* @ Version 1.0
*
Copyright (C), 2008 soda. c
*
This program is protected by copyright laws.
*
Program name: gamehall
* @ Data 2008-3-20
*/
Package org.sujun.net
{
Import flash.net. Socket;
Import flash. Events. progressevent;
Import flash. utils. bytearray;
/**
* The underlying processing in the socket data stream receives data from the server. when the conditions are met, the required bytes are thrown.
* Data in the data stream is not processed.
*
* @ Author soda. c
*/
Public class netbottomhandle
{
Private var socket: socket;
Private var listener: function; // receives parsed data from the network.
Private var msglen: int; // message length
Private var msglenmax: int; // maximum message length received
Private var headlen: int; // Message Header Length
Private var isreadhead: Boolean; // whether the message header has been read
Private var Bytes: bytearray; // buffer data of the read data. The read data is stored here.
Public Function netbottomhandle ()
{
Msglenmax = 5000; // 5000 bytes
Headlen = 4; // 4 bytes
Bytes = new bytearray ();
Isreadhead = true;
}
/**
* Set a network communication instance
*/
Public Function setsocket (socket: socket): void
{
This. Socket = socket;
// Listen ......
Socket. addeventlistener (progressevent. socket_data, onserverdata, false, 0, true );
}
/**
* Accept the raw data received at the network layer. The transmitted data is the description length and the bytearray object functon (Len, bytes)
* @ Param listener: receives data functions.
*/
Public Function receivernetdata (listener: function): void
{
This. Listener = listener;
}
/**
* The data sent by the server is received here, the lowest layer
*/
Private function onserverdata (Event: progressevent): void
{
// This function is used to check the data received.
Parsenetdata ();
}
/**
* Parse network data streams
*/
Private function parsenetdata (): void
{
// If you need to read the information Header
If (isreadhead)
{
If (socket. bytesavailable> = headlen)
{
// Read the indicator indicating the size of the subsequent data
Msglen = socket. readunsignedint ();
Isreadhead = false;
}
}
// If the information header has been read, check whether the number of bytes meeting the conditions can be received.
If (! Isreadhead & msglen <= msglenmax)
{
// If the value is 0, an exception message is received.
If (msglen = 0)
{
// If the message length is 0, it indicates that an error has occurred with the server, or the message is about to be disconnected. The client is notified for special processing.
Listener (msglen, null );
Return;
}
// The data in the data stream meets the conditions and starts to read the data.
If (socket. bytesavailable> = msglen)
{
// Pointer Regression
Bytes. Position = 0;
// Retrieve the network bytes of the specified length
Socket. readbytes (bytes, 0, msglen );
Listener (msglen, bytes );
Isreadhead = true;
}
}
// If the data stream still meets the Data Reading conditions, continue to read the data
If (socket. bytesavailable> = headlen)
{
Parsenetdata ();
}
}
}
}
I will not explain the code in detail, but I have made it very clear that this code has been honed by several versions I have written. Haha
In practical applications, the performance is also relatively stable.
Of course, this was an early stage. In the future, more processing may be required, such as the buffer and cache of each network byte, to avoid socket errors caused by too many bytes.
If you are interested, keep posting for discussion.
Netbottomhandle.rar
(1.51 KB)
Sample Code
Downloads: 2062008-6-29
Download silver 1 two
After modifying the code error, if you want to see the detailed process, including the server code, please read this article.
Http://www.sujun.org/article.asp? Id = 51