package
{importflash.display.Sprite;importflash.events.Event;importflash.events.MouseEvent;importflash.events.ProgressEvent;importflash.events.ServerSocketConnectEvent;importflash.net.ServerSocket;importflash.net.Socket;importflash.text.TextField;importflash.text.TextFieldType;importflash.utils.ByteArray;/**
* @author:Frost.Yen
* @E-mail:871979853@qq.com
* @create: 2016-7-20 下午12:37:30
**/
public class SocketServer extendsSprite
{private var serverSocket:ServerSocket = newServerSocket();private varclientSocket:Socket;private varlocalIP:TextField;private varlocalPort:TextField;private varlogField:TextField;private varmessage:TextField;private var _clients:Array =[];public functionSocketServer()
{
setupUI();
}//当客户端成功连接服务端
private function onConnect( event:ServerSocketConnectEvent):void{
clientSocket=event.socket;
clientSocket.addEventListener( ProgressEvent.SOCKET_DATA, onClientSocketData );
_clients.push(clientSocket);
trace(_clients.length);
log("Connection from " + clientSocket.remoteAddress + ":" +clientSocket.remotePort );
}//当有数据通信时
private function onClientSocketData( event:ProgressEvent ):void{var buffer:ByteArray = newByteArray();var client:Socket =event.currentTarget as Socket;
client.readBytes( buffer,0, client.bytesAvailable );
log("Received from Client"+ clientSocket.remoteAddress + ":" + clientSocket.remotePort+"-- " +buffer.toString() );
}//绑定服务器ip 开始监听端口
private function bind( event:Event ):void{if( serverSocket.bound )
{
serverSocket.close();
serverSocket= newServerSocket();
}
serverSocket.bind( parseInt( localPort.text ), localIP.text );
serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, onConnect );
serverSocket.listen();
log("Bound to: " + serverSocket.localAddress + ":" +serverSocket.localPort );
}//服务器端向所有客户端发送数据
private function send( event:Event ):void{try{if (_clients.length == 0)
{
log('没有连接');return;
}for (var i:int = 0; i < _clients.length; i++)
{var item:Socket =_clients[i] as Socket;if (!item) continue;
item.writeUTFBytes(message.text);
item.flush();
}
}catch( error:Error )
{
log( error.message );
}
}//输出日志
private function log( text:String ):void{
logField.appendText( text+ "\n");
logField.scrollV=logField.maxScrollV;
trace( text );
}//设置皮肤
private function setupUI():void{
localIP= createTextField( 10, 10, "Local IP", "0.0.0.0");
localPort= createTextField( 10, 35, "Local port", "0");
createTextButton(170, 60, "Bind", bind );
message= createTextField( 10, 85, "Message", "send message to Client.");
createTextButton(170, 110, "Send", send );
logField= createTextField( 10, 135, "Log", "", false, 200)this.stage.nativeWindow.activate();
}private function createTextField( x:int, y:int, label:String, defaultValue:String = '', editable:Boolean = true, height:int = 20):TextField
{var labelField:TextField = newTextField();
labelField.text=label;
labelField.type=TextFieldType.DYNAMIC;
labelField.width= 100;
labelField.x=x;
labelField.y=y;var input:TextField = newTextField();
input.text=defaultValue;
input.type=TextFieldType.INPUT;
input.border=editable;
input.selectable=editable;
input.width= 280;
input.height=height;
input.x= x +labelField.width;
input.y=y;this.addChild( labelField );this.addChild( input );returninput;
}private functioncreateTextButton( x:int, y:int, label:String, clickHandler:Function ):TextField
{var button:TextField = newTextField();
button.htmlText= "" + label + "";
button.type=TextFieldType.DYNAMIC;
button.selectable= false;
button.width= 180;
button.x=x;
button.y=y;
button.addEventListener( MouseEvent.CLICK, clickHandler );this.addChild( button );returnbutton;
}
}
}