package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.Socket;
import flash.system.Security;
import flash.display.MovieClip;
import flash.events.MouseEvent;
Security.loadPolicyFile("
*");
import fl.controls.Label;
import fl.controls.ColorPicker;
public class SocketExample extends Sprite {
private var socket:Socket;
private var tempMC:KingdaMC;
private var countNo:int;
private var aLabel

abel ;
private var bLabel

abel ;
private var aCp:ColorPicker ;
private var MSG:String;
public function SocketExample( ) {
aLabel=new Label();
bLabel=new Label();
addChild(aLabel);
addChild(bLabel);
aLabel.x = 200;
aLabel.y = 150;
aLabel.width = 550;
aLabel.height = 22;
bLabel.x = 200;
bLabel.y = 50;
bLabel.width = 550;
bLabel.height = 22;
socket = new Socket( );
countNo=0;
// Add an event listener to be notified when the connection
// is made
socket.addEventListener( Event.CONNECT, onConnect );
socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData);
// Connect to the server
socket.connect( "×",× );
tempMC=new KingdaMC ();
addChild(tempMC);
this.addEventListener(MouseEvent.CLICK, clickHandler2);
this.addEventListener(MouseEvent.MOUSE_MOVE, clickHandler);
MSG ="Hello Everybody!";
//+int(Math.random()*10000);
}
private function onConnect( event:Event ):void {
socket.writeUTFBytes("CONN|Hello Everybody!");
socket.flush();
trace( "The socket is now connected..." );
}
private function sendMSG(event:MouseEvent):void {
//trace("You clicked the ball");
tempMC.x=100;
countNo++;
MSG="CHAT|You clicked the ball"+countNo;
socket.writeUTFBytes(MSG);
socket.flush();
bLabel.text=MSG;
}
private function closesocket(event:MouseEvent):void {
socket.writeUTFBytes("GONE");
socket.flush();
socket.close();
}
private function onSocketData(eventrogressEvent ):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:String = socket.readUTFBytes(socket.bytesAvailable);
aLabel.text =data;
}
}
}
}
flash自己带的例子