[ActionScript 3.0] AS3.0 Socket通信实例
以下类是充当Socket服务器的例子
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.ServerSocketConnectEvent;
import flash.net.ServerSocket;
import flash.net.Socket;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.utils.ByteArray; /**
* @author:Frost.Yen
* @E-mail:871979853@qq.com
* @create: 2016-7-20 下午12:37:30
*
*/
public class SocketServer extends Sprite
{
private var serverSocket:ServerSocket = new ServerSocket();
private var clientSocket:Socket;
private var localIP:TextField;
private var localPort:TextField;
private var logField:TextField;
private var message:TextField;
private var _clients:Array = []; public function SocketServer()
{
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 = new ByteArray();
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 = new ServerSocket(); }
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 = new TextField();
labelField.text = label;
labelField.type = TextFieldType.DYNAMIC;
labelField.width = 100;
labelField.x = x;
labelField.y = y; var input:TextField = new TextField();
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 ); return input;
}
private function createTextButton( x:int, y:int, label:String, clickHandler:Function ):TextField
{
var button:TextField = new TextField();
button.htmlText = "<u><b>" + label + "</b></u>";
button.type = TextFieldType.DYNAMIC;
button.selectable = false;
button.width = 180;
button.x = x;
button.y = y;
button.addEventListener( MouseEvent.CLICK, clickHandler ); this.addChild( button );
return button;
}
}
}
以下类是充当客户端的例子
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.utils.ByteArray; /**
* @author:Frost.Yen
* @E-mail:871979853@qq.com
* @create: 2016-7-20 下午12:44:52
*
*/
public class SocketClient extends Sprite
{
private var clientSocket:Socket = new Socket();
private var localIP:TextField;
private var localPort:TextField;
private var logField:TextField;
private var message:TextField;
private var _clients:Array = [];
public function SocketClient()
{
setupUI(); }
private function onConnect( event:Event ):void
{
log('成功连接服务器!');
log( "Connection from " + clientSocket.remoteAddress + ":" + clientSocket.remotePort );
}
private function onClientSocketData( event:ProgressEvent):void
{
var buffer:ByteArray = new ByteArray();
clientSocket.readBytes( buffer, 0, clientSocket.bytesAvailable );
log( "Send: " + buffer.toString() );
}
private function collect(e:Event):void
{
bind(localIP.text, parseInt( localPort.text ));
}
public function bind(host:String = "localhost", port:Number = 9080):void
{
log('开始连接服务器!');
clientSocket.connect(host, port);
clientSocket.addEventListener(Event.CONNECT, onConnect);//监听连接事件
clientSocket.addEventListener(IOErrorEvent.IO_ERROR,onError);
clientSocket.addEventListener(ProgressEvent.SOCKET_DATA,onSocketData);
}
private function onError(e:IOErrorEvent):void
{
log(e.toString());
}
//向服务器发送数据
private function send( event:Event ):void
{
try
{
if( clientSocket != null && clientSocket.connected )
{
clientSocket.writeUTFBytes( message.text );
clientSocket.flush();
//log( "Sent message to " + clientSocket.remoteAddress + ":" + clientSocket.remotePort );
}
else log("No socket connection.");
}
catch ( error:Error )
{
log( error.message );
}
}
private function onSocketData(e:ProgressEvent):void
{
var buffer:String = clientSocket.readUTFBytes(clientSocket.bytesAvailable );
log( "Received from Server:" + buffer );
}
private function log( text:String ):void
{
logField.appendText( text + "\n" );
logField.scrollV = logField.maxScrollV;
} private function setupUI():void
{
localIP = createTextField( 10, 10, "Local IP", "0.0.0.0");
localIP.text = 'localhost';
localPort = createTextField( 10, 35, "Local port", "0" );
createTextButton( 170, 60, "Collect", collect );
message = createTextField( 10, 85, "Message", "Lucy can't drink milk." );
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 = new TextField();
labelField.text = label;
labelField.type = TextFieldType.DYNAMIC;
labelField.width = 100;
labelField.x = x;
labelField.y = y; var input:TextField = new TextField();
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 ); return input;
} private function createTextButton( x:int, y:int, label:String, clickHandler:Function ):TextField
{
var button:TextField = new TextField();
button.htmlText = "<u><b>" + label + "</b></u>";
button.type = TextFieldType.DYNAMIC;
button.selectable = false;
button.width = 180;
button.x = x;
button.y = y;
button.addEventListener( MouseEvent.CLICK, clickHandler ); this.addChild( button );
return button;
} }
}
[ActionScript 3.0] AS3.0 Socket通信实例的更多相关文章
- Flex通信-与Java实现Socket通信实例
Flex通信-与Java实现Socket通信实例 转自:http://blessht.iteye.com/blog/1136888 博客分类: Flex 环境准备 [服务器端] JDK1.6,“ja ...
- Linux下简单的socket通信实例
Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Br ...
- (8)Linux(客户端)和Windows(服务端)下socket通信实例
Linux(客户端)和Windows(服务端)下socket通信实例: (1)首先是Windows做客户端,Linux做服务端的程序 Windows Client端 #include <st ...
- Java Socket 通信实例 - 转载
基于Tcp协议的简单Socket通信实例(JAVA) 好久没写博客了,前段时间忙于做项目,耽误了些时间,今天开始继续写起~ 今天来讲下关于Socket通信的简单应用,关于什么是Socket以及一些 ...
- 网络协议栈学习(一)socket通信实例
网络协议栈学习(一)socket通信实例 该实例摘自<linux网络编程>(宋敬彬,孙海滨等著). 例子分为服务器端和客户端,客户端连接服务器后从标准输入读取输入的字符串,发送给服务器:服 ...
- [ActionScript 3.0] AS3.0和AS2.0的相互通信
AS3和AS2之间的通信,最好的方式可能就是LocalConnection了. AS2向AS3发送数据,即AS2调用AS3的函数: as2.0代码(按钮上写的发送信息代码): on (release) ...
- [ActionScript 3.0] AS3.0 让一个视频无缝循环播放的一个偏方
一个视频要循环播放,通常的办法都是播放完后再重新播放,但是不可避免的播放结束和重新开始播放这个时间点上会有停顿,如何解决这个问题,说个偏方吧! package com.views { import f ...
- [ActionScript 3.0] AS3.0 下雨及涟漪效果
帧代码: stage.frameRate = 80; function init(x1:Number,y1:Number) { var mc:MovieClip=new MovieClip(); ad ...
- 简单的C# Socket通信实例
一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息:连接使用的协议,本 ...
- Java Socket通信实例
一.简单的客户端与服务器一对一连接: Socket通信的步骤: 1.创建ServerSocket和Socket 2.打开连接到Scket的输入/输出流 3.按照协议对Socket进行读/写操作 4.关 ...
随机推荐
- Spring控制多张表的提交事务操作
一.Spring配置文件如下: <bean id="test" class="org.apache.commons.dbcp.BasicDataSource&quo ...
- html 选择图片后马上展示出来
document.getElementById('file4').onchange = function(evt) { // 如果浏览器不支持FileReader,则不处理 if (!window.F ...
- sql 跨域
1. 开通分布式查询权限 reconfigure reconfigure 2. 查询 ',NETACS.dbo.tb_car) a select * from opendatasource('SQLO ...
- iOS开发中的数据安全隐患和解决方案
移动互联网的兴起,每天都会涌现大量的app产品,无论公司开发还是个人开发,大家都在追求更快的抢占市场,但是确忽略了打磨产品,也忽略了移动开发中的数据安全隐患,如果我们用Charles工具,很容易截获到 ...
- Mark一下,一上午就这么过去了,关于客户端连接oracle10G的问题
Mark一下,一上午就这么过去了,关于客户端连接oracle10G的问题 正常的客户端PLSQL和Navicat都可以正常连接Oracle(局域网内),但代码生成器和VS2015死活连不上,在网上找了 ...
- POJ-1182 分组并查集
今天刚发现,之前做的并查集只是贴模板基本就能过,题意改变一点,自己还是不懂,其实我还没入门呢... 题意:食物链,A吃B,B吃C,C吃A,输入m组数据: 1 a b:a 和 b 是同一类 2 a b: ...
- UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)
题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...
- HDU 1536 & 1944
http://acm.hdu.edu.cn/showproblem.php?pid=1536 http://acm.hdu.edu.cn/showproblem.php?pid=1944 一样的题 题 ...
- 纯CSS3实现轮播切换效果
使用纯css3实现与轮播器一样的功能. HTML代码: <div class="slide-container"> <input type="radio ...
- windows调试器尝鲜
曾几何时,我也下载过看雪论坛精华看的津津有味.可惜一直没有动手去调试,学到的x86汇编指令也忘得差不多了.最近将老机器的T4200 CPU换成了更省电,温度更低的P8800,为了支援新的VT虚拟化,特 ...