[转] flash socket与php socket通信的例子

[url]http://bbs.9ria.com/viewthread.php?tid=76152&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000[/url]

使用说明:
1:把socket这个文件夹复制到硬盘,比如盘符是d:\
2:找到socket\php下面的start.bat,右键--编辑---修改盘符(这里默认地址是d:\)
3:把socket文件夹下的xx.cfg复制到C:\Documents and Settings\hui.wang\Application Data\Macromedia\Flash Player\#Security\FlashPlayerTrust下。
4:双击php\socket下的start.bat,运行服务端。可以查看服务端运行状况。
5:双击flash\load.swf运行,点击舞台,可以收到服务端数据。
6:Server.php里已经默认发送crossdomain.xml字符,防止安全沙箱冲突。
flash 代码:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.XMLSocket;
import flash.text.TextField;
import flash.utils.ByteArray;

public class Load extends Sprite
{

protected var sock:Socket;

protected var reads:ByteArray;

protected var isConnection:Boolean;

private var _tx:TextField;

public function Load()
{
this._init();

}
private function _init():void
{
sock=new Socket();

sock.addEventListener(Event.CONNECT,onConnect);

sock.addEventListener(ProgressEvent.SOCKET_DATA,onData);

sock.addEventListener(Event.CLOSE,onClose);

sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onError);

sock.addEventListener(IOErrorEvent.IO_ERROR,onIOError);

sock.connect("localhost",4324);

this.stage.addEventListener(MouseEvent.CLICK,onClick);

this._tx=new TextField();

addChild(this._tx);

this._tx.border=true;

this._tx.multiline=true;

}
public function onClick(ev:MouseEvent):void
{
if(this.isConnection)
{
var mes:String="flash"+Math.floor(Math.random()*1000000);

this.sendMes(mes);

trace("发送给服务端数据是: ",mes);

}else
{
trace("服务未被连接!");
}
}
public function sendMes(mes:String):void
{
var bytes:ByteArray=new ByteArray();

bytes.writeUTFBytes(mes+"\n");

bytes.position=0;

sock.writeBytes(bytes);

sock.flush();

}
public function onConnect(ev:Event):void
{
trace("连上服务端");

this.isConnection=true;

}
public function onData(ev:ProgressEvent):void
{
trace("收到服务器发来的数据");

if(sock.bytesAvailable)
{
reads=new ByteArray();

sock.readBytes(reads);

trace(reads);

_tx.appendText(" "+reads);



}
}
public function onClose(ev:Event):void
{
trace("服务被关闭");

this.isConnection=false;

}
public function onError(ev:SecurityErrorEvent):void
{
trace("沙箱错误");
}
public function onIOError(ev:IOErrorEvent):void
{
trace("接口错误,请检查ip和端口");
}

}
}


//-------------服务端代码:
Server.php
<?php
/**
* Created on Mar 9, 2011
* @auth hui.wang
* This Class is Server
*/
class Server
{
public $host;

public $port;

public $isListen=true;

protected $sock;

protected $bind;

protected $listen;

protected $connection;

protected $input;

protected $output;

public $crossdomain;

public function __construct($ip=null,$entrance=null)
{
$this->host=$ip;

$this->port=$entrance;

$this->setCrossDomain();

}
/**socket开始*/
public function start()
{
$this->serverLoop();

}
/**关闭socket*/
protected function shutdown()
{
socket_close($this->connection);

socket_close($this->sock);

}
protected function serverLoop()
{
$this->sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

if($this->sock)
{
echo "Socket has been created \n";


}else
{
echo "failed to create socket".socket_strerror($this->sock)."\n";

exit();

}
$this->bind=socket_bind($this->sock,$this->host,$this->port);

if($this->bind)
{
echo "Socket has been bind \n";

}else
{
echo "failed to bind socket: ".socket_strerror($this->bind)."\n";

exit();
}

$this->listen=socket_listen($this->sock,5);

if($this->listen)
{
echo "Socket is listening \n";

}else
{
echo "failed to listen to socket: ".socket_strerror($this->listen)."\n";

exit();
}

echo "waiting for clients to connect \n";

while($this->isListen)
{
$this->connection=socket_accept($this->sock);

if(!$this->connection)
{
usleep(100);

}else if($this->connection>0)
{
echo "A connection...\n";

$this->handleClient();

}

}
$this->shutdown();

}
/**设置crossdomain.xml字符串*/
protected final function setCrossDomain()
{
$this->crossdomain="<cross-domain-policy>" .
"<allow-access-from domain='*' to-ports='*' />" .
"</cross-domain-policy>" .
"\0";
}
/**处理客户端*/
protected function handleClient()
{
while($this->input=socket_read($this->connection,2048))
{
if(strpos($this->input,'policy-file-request'))
{
socket_write($this->connection,$this->crossdomain,strlen($this->crossdomain));

}else
{
$this->input=trim($this->input);

echo "Recived mes: ".$this->input."\n";

$this->output=strrev($this->input)."\n";

echo "Back to client mes:(len is ".strlen($this->output).") ".$this->output."\n";

socket_write($this->connection,$this->output,strlen($this->output))or die("Could not write output\n");
}

}
socket_close($this->connection);

}

}
?>


start.php代码

<?php
/*
* Created on Mar 9, 2011
* @auth hui.wang
*/
include_once 'Server.php';

$server=new Server("localhost",4324);

$server->start();

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值