Original address: http://www.asp119.com/news/2009522181815_1.htm
The method of receiving data from the socket depends on whether you use the socket type, the socket and the Xmlsocket can receive the data, but the implementation method is somewhat different, let's first look at how the socket class is done.
As mentioned above, Flash provides the socket communication method is asynchronous communication, that is, simply create a socket connection and try to read the data this is not possible, the Read method reads the data does not wait for data transfer to come back immediately, If the data is not ready to read the data will cause an exception.
When the data is ready, the Socketdata event is triggered by registering the event handler, which is triggered when the data is sent over, so that the data can be read by the processing function.
To read the data sent by the server, the socket class provides a series of read methods for reading different types of data, such as reading a byte through the ReadByte () method, and reading an unsigned integer using the Readunsignedint () method, depending on the table below:
Table 24-1. Socket read methods for various datatypes
method: Return type
Description
Number of bytesReadboolean (): Boolean read Boolean Data 1readByte (): int reads one byte of data 1readDouble (): Number reads IEEE 754 double precision floating point number 8readFloat (): Number read IEEE 754 single-precision floating-point number 4readInt (): In read 32-bit integer 4readObject ():* read AMF Format Object Nreadshort (): int reads 16-bit integer 2readUnsignedByte (): UINT Read unsigned byte 1readUnsignedInt (): UINT reads unsigned 32-bit integer 4readUnsignedShort (): UINT reads unsigned 16-bit integer 2readUTF (): string reads UTF-8 string n
There are two other methods not in the table above, they are readbytes () and Readutfbytes (), the Readbytes () method has no return value, it accepts three parameters:
bytes
A Flash.util.ByteArray instance populates the read data
Offset
A UINT value that specifies the offset of the read data, which defaults to 0
Length
A UINT value represents the number of bytes read, the default is 0, which represents all data
The Readutfbytes () method takes only one parameter, which represents the number of UTF-8 bytes read, and returns a string.
The following example code connects a socket server and reads and displays the data sent by the server:
Package {
Import Flash.display.Sprite;
Import flash.events.ProgressEvent;
Import Flash.net.Socket;
public class Socketexample extends Sprite {
private Var Socket:socket;
Public Function Socketexample () {
Socket = new socket ();
Listen for if data is received from the socket server
Socket.addeventlistener (Progressevent.socket_data, onsocketdata);
Connect to the server
Socket.connect ("localhost", 2900);
}
Private Function Onsocketdata (event:progressevent): void {
Trace ("Socket received" + Socket.bytesavailable + "byte (s) of data:");
Loop over all of the received data, and only read a byte if there
is one available
while (socket.bytesavailable) {
Read a byte from the socket and display it
var data:int = Socket.readbyte ();
Trace (data);
}
}
}
}
In the example above, if the socket server sends a "Hello" string, the output is:
The socket receives 5 bytes of data:
72
101
108
108
111
The data received by the socket object is ASCII encoded text, we can reconstruct the string using the Readutfbytes () method, the Readutfbytes () method needs to know how many bytes need to be converted, and specifies the number of bytes with the bytesavailable attribute:
var string:string = socket.readutfbytes (socket.bytesavailable);
The Xmlsocket class and the socket class are basically similar, both to register the Jian listener to detect whether the data is received, but the two ways to read the data are different.
When the data is ready, the Xmlsocket instance emits a data event with the event type Flash.events.DataEvent.DATA, where the Data property contains the received information.
The data returned from the server is the raw data, and if you want to work with XML, you need to convert the data to an XML instance first.
The following code example connects the local server with Xmlsocket, the port is 2900, the connection succeeds, the Send <test> message to the server, the OnData event handler processes the data returned by the server, and returns the data as <response>< Test success= ' true '/></response> Note that the contents of the Data property of the event are only string data, use the XML constructor to convert the bit XML instance, and finally output the XML through the E4X syntax:
Package {
Import Flash.display.Sprite;
Import flash.events.Event;
Import flash.events.DataEvent;
Import Flash.net.XMLSocket;
public class Socketexample extends Sprite {
private Var Xmlsocket:xmlsocket;
Public Function Socketexample () {
XmlSocket = new XmlSocket ();
Connect Listener to send a message to the server
After we make a successful connection
Xmlsocket.addeventlistener (Event.connect, onConnect);
Listen for if data is received from the socket server
Xmlsocket.addeventlistener (Dataevent.data, onData);
Connect to the server
Xmlsocket.connect ("localhost", 2900);
}
Private Function OnConnect (event:event): void {
Xmlsocket.send ("<test/>");
}
Private Function OnData (event:dataevent): void {
The raw string returned from the server.
It might look something like this:
<response><test success= ' true '/></response>
Trace (Event.data);
Convert the string into XML
var response:xml = new XML (event.data);
Using e4x, access the success attribute of the "test"
Element node in the response.
Output:true
Trace ([email protected]);
}
}
}
Collected in 2011-05-24 from the Baidu space
ActionScript receives data sent from the socket server