可以使用 XMLSocket 類的XMLSocket.connect()和XMLSocket.send()方法,通過套接字連線與伺服器之間傳輸 XML。XMLSocket.connect()方法與 Web 伺服器埠建立套接字連線。XMLSocket.send()方法將 XML 物件傳遞給套接字連線中指定的伺服器。
要建立套接字連線,必須建立伺服器端應用程式以等待套接字連線請求,並將響應傳送到 Flash Player 或 AIR 應用程式。此類伺服器端應用程式可使用 AIR 或其他程式語言(如 Java、Python 或 Perl)編寫。要使用 XMLSocket 類,伺服器計算機必須執行可識別 XMLSocket 類使用的簡單協議的守護程式:
package
{
import flash.display.Sprite;
import flash.events.DatagramSocketDataEvent;
import flash.events.Event;
import flash.net.DatagramSocket;
import flash.utils.ByteArray;
public class DatagramSocketExample extends Sprite
{
private var datagramSocket:DatagramSocket;
//The IP and port for this computer
private var localIP:String = "192.168.0.1";
private var localPort:int = 55555;
//The IP and port for the target computer
private var targetIP:String = "192.168.0.2";
private var targetPort:int = 55555;
public function DatagramSocketExample()
{
//Create the socket
datagramSocket = new DatagramSocket();
datagramSocket.addEventListener( DatagramSocketDataEvent.DATA, dataReceived );
//Bind the socket to the local network interface and port
datagramSocket.bind( localPort, localIP );
//Listen for incoming datagrams
datagramSocket.receive();
//Create a message in a ByteArray
var data:ByteArray = new ByteArray();
data.writeUTFBytes("Hello.");
//Send the datagram message
datagramSocket.send( data, 0, 0, targetIP, targetPort);
}
private function dataReceived( event:DatagramSocketDataEvent ):void
{
//Read the data from the datagram
trace("Received from " + event.srcAddress + ":" + event.srcPort + "> " +
event.data.readUTFBytes( event.data.bytesAvailable ) );
}
}}