Transfer from http://520pig.blogbus.com/logs/32821429.html
Socket socket connection allows Flash player by specifying the port communicate with the server, but the socket after the data transfer communication is still not close to artificially close. We can use the class to create XML data format flash.net.XMLSocket the socket connection, use the class to create a binary data format flash.net.Socket socket connection, compared with xml data format, binary link is more low-level, but it can connect to almost all socket server-side program. as3.0 socket provided in the link are asynchronous, meaning that you must increase the event listener later in the transfer complete data available
To create a socket communication, we must first establish socket connection, we can use Socket.connect () or XMLSocket.connect () method to establish a connection and connect the event monitor to determine whether the established connection. Connect only two parameters, IP, and port.
swf and the host must be in the same domain;
Swf on the network can not connect to local server;
Swf can not access any local network resources;
To allow cross-domain access or connection ports lower than 1024, the need for cross-domain policy file.
If you want to visit each other in different domains of the flash can flash.system.Security.loadPolicyFile () to read:
cross-domain policy file example:
<? Xml version = "1.0"?>
<! DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domainpolicy.
<allow-access-from domain="*" to-ports="80,110" />
End connections can be established after the write server, by calling the write method, you can write to write data to the buffer pool, then call the flush () method can be sent to the data, this is for the binary socket, if xml call send () method. as3.0 can define different parameters for different write function: is writeBoolean (), writeByte (), writeBytes (),
writeDouble (), writeFloat (), writeInt (), writeMultiByte (), writeObject (), writeShort (), write-
UnsignedInt (), writeUTF (), and writeUTFBytes (). According to the function name can be aware of the need incoming parameters, here on socket.writeBytes (byteArray, 0, byteArray.length); to explain, because it can pass a data, and develop an array of start and end position for writing. , WriteUTFBytes (): write byte string, writeUTF: numeric string. writeMultiByte () to write to the specified encoding string, socket.writeMultiByte ("example", "unicode");
Once the data would have to send data, so we can ProgressEvent.SOCKET_DATA event handler to read the data, using readBoolean (): Boolean, readByte (): int, readDouble (): Number, readFloat (): Number, readInt (): int, readObject ():*, readShort (): int, readUnsignedByte (): uint, readUnsignedInt (): uint, readUnsignedShort (): uint, readUTF (): String, readBytes () no return parameters, readUTFBytes ( ) return utf8 data. Socket object receives the data is ASCII-encoded text, we can use readUTFBytes () method to reconstruct the string
readUTFBytes () method needs to know how many bytes need to convert, with bytesAvailable property specifies the number of bytes:
var string: String = socket.readUTFBytes (socket.bytesAvailable);
Mentioned above, the socket is in binary transfer mode, if the use xml approach, then the trigger event types, it is DataEvent.DATA trigger event, the event's data property contains the data passed to, if we are to read xml data must first be converted to string in xml format, and then read.