[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.关 ...
随机推荐
- 移动端web之像素基础
px:css pixels逻辑像素,浏览器使用的抽象单位 dp,pt :device independent pixels 设备无关像素 dpr:devicePixelRatio 设备像素缩放比 计算 ...
- TreeSet和Comparator 对TreeSet排序
使用TreeSet和Comparator,编写TreeSetTestInner类,要求对TreeSet中的元素"HashSet"."ArrayList".&qu ...
- 用dbforge调试procedure
工具官网地址:http://www.devart.com/dbforge/mysql/studio/ 对于某些存储过程很多且复杂的SQL的应用,在短时间内要使得所有MySQL存储过程和函数正常运行,那 ...
- Maven配置中scope说明
Maven环境搭建完成后,需要去pom.xml文件中配置相关使用的jar架包. 如上图,架包选定之后需要配置对应的scope属性,下面来简单说下这些属性选项的含义: 1. compile,缺省值,适用 ...
- JBoss集群中启用HTTPS协议
Generate server certificate Note: If you already have certificate created then this section can be i ...
- servlet执行流程
视频地址:http://www.imooc.com/video/5550 1-6 用户输入地址:localhost:8080/MyFirstServletDemo/index.jsp (My ...
- Annotation注解(有源代码)
注解(annotation)概述: ·从JDK5.0 开始,java增加了对元数据(MetaData)的支持,也就是Annotation(注解) ·Annotation其实就是代码里的特殊标记,这些标 ...
- cassandra 环境搭建
1 下载安装包 http://www.planetcassandra.org/cassandra/?dlink=http://downloads.datastax.com/community/dsc- ...
- 从零开始学习Node.js例子七 发送HTTP客户端请求并显示响应结果
wget.js:发送HTTP客户端请求并显示响应的各种结果 options对象描述了将要发出的请求.data事件在数据到达时被触发,error事件在发生错误时被触发.HTTP请求中的数据格式通过MIM ...
- solr导入数据库数据-tinyint数据转boolean
solr在导入mysql数据库数据时,遇到一个问题:数据类型为tinyint的数据对应不到solr中的数据类型,因此就需要将tinyint转一下. 修改schema.xml文件,如下: 1.添加boo ...