以下类是充当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通信实例的更多相关文章

  1. Flex通信-与Java实现Socket通信实例

    Flex通信-与Java实现Socket通信实例  转自:http://blessht.iteye.com/blog/1136888 博客分类: Flex 环境准备 [服务器端] JDK1.6,“ja ...

  2. Linux下简单的socket通信实例

    Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Br ...

  3. (8)Linux(客户端)和Windows(服务端)下socket通信实例

    Linux(客户端)和Windows(服务端)下socket通信实例: (1)首先是Windows做客户端,Linux做服务端的程序 Windows   Client端 #include <st ...

  4. Java Socket 通信实例 - 转载

    基于Tcp协议的简单Socket通信实例(JAVA)   好久没写博客了,前段时间忙于做项目,耽误了些时间,今天开始继续写起~ 今天来讲下关于Socket通信的简单应用,关于什么是Socket以及一些 ...

  5. 网络协议栈学习(一)socket通信实例

    网络协议栈学习(一)socket通信实例 该实例摘自<linux网络编程>(宋敬彬,孙海滨等著). 例子分为服务器端和客户端,客户端连接服务器后从标准输入读取输入的字符串,发送给服务器:服 ...

  6. [ActionScript 3.0] AS3.0和AS2.0的相互通信

    AS3和AS2之间的通信,最好的方式可能就是LocalConnection了. AS2向AS3发送数据,即AS2调用AS3的函数: as2.0代码(按钮上写的发送信息代码): on (release) ...

  7. [ActionScript 3.0] AS3.0 让一个视频无缝循环播放的一个偏方

    一个视频要循环播放,通常的办法都是播放完后再重新播放,但是不可避免的播放结束和重新开始播放这个时间点上会有停顿,如何解决这个问题,说个偏方吧! package com.views { import f ...

  8. [ActionScript 3.0] AS3.0 下雨及涟漪效果

    帧代码: stage.frameRate = 80; function init(x1:Number,y1:Number) { var mc:MovieClip=new MovieClip(); ad ...

  9. 简单的C# Socket通信实例

    一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息:连接使用的协议,本 ...

  10. Java Socket通信实例

    一.简单的客户端与服务器一对一连接: Socket通信的步骤: 1.创建ServerSocket和Socket 2.打开连接到Scket的输入/输出流 3.按照协议对Socket进行读/写操作 4.关 ...

随机推荐

  1. alter table的用法

    在数据库开发过程中,除了用得最多的数据库查询外,我们有时也需要去修改数据表的定义,比如在已存在的数据表中新增列和删除列等.这篇文章就总结一下alter table语句的用法. 示例代码如下. USE ...

  2. Python排序算法

    不觉已经有半年没写了,时间真是容易荒废,这半年过了个春节,去拉萨旅行.本职工作也很忙,没有开展系统的学习和总结. 今年开始静下心来从基础开始学习,主要分为三部分,算法.线性代数.概率统计. 首先学习算 ...

  3. Sql获取第一天、最后一天

    昨天面试一家公司,上机题目中要求获取每月最后一笔订单.用到了日期的选择性查询,回来在ITeye上找到了这篇文章. 原文: http://new-fighter.iteye.com/blog/17587 ...

  4. CSS3-Media Query 基础

    一.常见的属性: device-width , device-height 屏幕宽高 width , height 渲染窗口宽高 orientation 设备方向 resolution 设备分辨率 二 ...

  5. Oracle的内存结构

    备注:本图片截图自“炼数成金” Oracle的体系结构分为内存结构.进程,磁盘文件. 内存结构分为SGA, PGA.SGA是系统全局区,是所有的用户共享区,PGA是某个用户的私有区. SGA分为sha ...

  6. JS中的 变量提升

    首先纠正下,文章标题里的 “变量提升” 名词是随大流叫法,“变量提升” 改为 “标识符提升” 更准确.因为变量一般指使用 var 声明的标识符,JS 里使用 function 声明的标识符也存在提升( ...

  7. Apache2.2与php5.17 mysql配置

    php5.217应该用线程安全搬,不然各种无语的Apache打不开,PHPInfo没有Mysql的信息,记得把php.ini放入系统盘Windows目录下,Win764位的libmysql.dll也放 ...

  8. sqlserver08评估期已过的解决方法

    打开sqlserver出现提示:评估期已过.有关如何升级的测试版软件的信息,请访问http://www.microsoft.com/sql/howtobuy 解决方法如下: 第一步:进入开始菜单--- ...

  9. C语言处理xml文件的库

    读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...

  10. Spark SQL Thrift Server 配置 Kerberos身份认证和权限管理

    转载请注明出处:http://www.cnblogs.com/xiaodf/ 之前的博客介绍了通过Kerberos + Sentry的方式实现了hive server2的身份认证和权限管理功能,本文主 ...