关于socket不多说了,这个网上的资料很多,关于flash的资料也很多,这里只说在网上找不到资料或者资料很少的哦东西
php的socket资料可谓少之又少,光是在google上搜php socket,出来的结果页就是那么三四篇文章,点进去看看,基本都是
从手册上抄下来的,一点问题都起不了,以至于我花了两天时间才使php和flash成功通信,呵呵
其实,php和flash的socket通信不是问题的难点,难点在于flash的安全策略,特别是socket,特别是在flash player 10中要求更加
严格.
下面分几个部分来说:php的socket\flash的socket\flash的安全策略\怎么用php解决这个策略
希望对缺乏资料的人有所帮助,如果你用的其他后台语言与flash交互,可能比php简单,因为php的确不是个做socket的好东西,
但是或许某个时刻你就会用到这个
(1)php的socket:
先贴一段代码,就是我实现通信的程序中的代码:
- <?php
- set_time_limit(0);
- $address = "127.0.0.1";
- ob_implicit_flush();
- $port = '8083';
- if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
- echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n";
- }
- if (($ret = socket_bind($sock, $address, $port)) < 0) {
- echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
- }
- if (($ret = socket_listen($sock, 5)) < 0) {
- echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
- }
- do {
- if (!($msgsock = socket_accept($sock))) {
- echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
- break;
- }
- do {
- //如果是安全策略请求,则传输安全策略文件内容
- if($buf = socket_read($msgsock, 2048)){
- if(strpos($buf,'policy-file-request')){
- $msg ="<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>";
- socket_write($msgsock, $msg."\0", strlen($msg."\0"));
- }
- //答复数据
- $talkback = "PHP: You said '$buf'.\n";
- socket_write($msgsock, $talkback, strlen($talkback));
- }
- } while (true);
- socket_close($msgsock);
- } while (true);
- //socket_close($spawn);
- //socket_close($socket);
- ?>
复制代码
(2)flash的socket通信:
flash的socket通信也是资料比较多,直接贴代码
- package
- {
- import fl.controls.TextArea;
- import fl.core.UIComponent;
- import flash.events.*;
- import flash.net.Socket;
- import flash.system.*;
- import flash.utils.ByteArray;
- import flash.utils.setTimeout;
- public class CustomSocket
- {
- private const CR:int = 13; // Carriage Return (CR)
- private const WILL:int = 0xFB; // 251 - WILL (option code)
- private const WONT:int = 0xFC; // 252 - WON'T (option code)
- private const DO:int = 0xFD; // 253 - DO (option code)
- private const DONT:int = 0xFE; // 254 - DON'T (option code)
- private const IAC:int = 0xFF; // 255 - Interpret as Command (IAC)
- private var serverURL:String;
- private var portNumber:int;
- private var socket:Socket;
- private var ta:TextArea;
- private var state:int = 0;
- System.useCodePage = false;
- public function CustomSocket(server:String, port:int, output:TextArea)
- {
- serverURL = server;
- portNumber = port;
- ta = output;
- socket = new Socket();
- socket.addEventListener(Event.CONNECT, connectHandler);
- socket.addEventListener(Event.CLOSE, closeHandler);
- socket.addEventListener(ErrorEvent.ERROR, errorHandler);
- socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
- socket.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);
- // Security.loadPolicyFile("xmlsocket://" + serverURL + ":"+portNumber);
- try
- {
- msg("Trying to connect to" + serverURL + ":" + portNumber + "\n");
- socket.connect(serverURL,portNumber);
- }
- catch (error:Error)
- {
- msg(error.message + "\n");
- // socket.close();
- }
- }
- public function ioErrorHandler(event:IOErrorEvent):void
- {
- msg("Unable to connect: socket error.\n");
- }
- public function writeBytesToSocket():void {
- socket.writeUTFBytes("sssssssssssssssssssss");
- socket.flush();
- }
- private function connectHandler(event:Event):void {
- if (socket.connected) {
- msg("connected...\n");
- } else {
- msg("unable to connect\n");
- }
- }
- private function closeHandler(event:Event):void
- {
- msg("closed...\n");
- }
- private function errorHandler(event:ErrorEvent):void {
- msg(event.text + "\n");
- }
- private function dataHandler(event:ProgressEvent):void {
- while ( socket.bytesAvailable ) {
- // Read a byte from the socket and display it
- var data = socket.readUTFBytes(socket.bytesAvailable);
- ta.text+=data;
- }
- }
- private function msg(value:String):void {
- ta.text += value;
- ta.dispatchEvent(new Event(Event.CHANGE));
- setTimeout(setScroll, 100);
- }
- public function setScroll():void {
- ta.verticalScrollPosition = ta.maxVerticalScrollPosition;
- }
- public function closeSocket():void{
- socket.close();
- }
- public function sendM(e:String):void
- {
- socket.writeUTFBytes(e);
- socket.flush();
- }
- }
- }
复制代码
(3).安全策略
基本内容见此文章:http://wangleifire.iteye.com/blog/335034
flash的安全策略是很严格的,特别是在flash 10中,当使用socket的时候必须存在策略文件才能继续传输数据,这个文件以前可以直接存放在根目录即可,而现在要求必须
在socket中直接传输才行,默认情况下flash会在服务器的843端口寻找这个策略文件的传输socket,可是php中开多个端口传输很有问题,不能实现多线程是主要问题,所以
这个方法不太好,也不太直接,其实还有一个非常直接的方法,就是不用任何端口或者开辟其他通信,flash在向某个端口请求数据的时候,第一次会发送一个字符串
"<policy-file-request/>",如果服务器收到这个字符窜,直接回复一个策略文件格式的字符串,即可通过安全验证,然后就可以传输数据了,我做过实验,的确可以通信了,
但是因为是php,一直执行某个网页才能监听socket,所以不能放在服务器上给大家来测试,如果感兴趣可以找我探讨:qq:676588498;
- if($buf = socket_read($msgsock, 2048)){
- if(strpos($buf,'policy-file-request')){
- $msg ="<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>";
- socket_write($msgsock, $msg."\0", strlen($msg."\0"));
- }
- //答复数据
- $talkback = "PHP: You said '$buf'.\n";
- socket_write($msgsock, $talkback, strlen($talkback));
- }
复制代码
上面这部分php代码正是实现了安全策略文件传送的作用,这个是很重要的,没有这句判断的话,在flash ide里测试的话或者在flex builder里测试的话是可以正常通信的,但是
当你将flash放在服务器上或者独立运行的时候就会无法连接服务器,提示安全策略失败什么的,这是因为在测试环境中,Adobe忽略了策略文件请求这一部,可以方便开发者,但是
也容易让人忽视安全问题.o了,罗嗦到这,今晚又熬过头了 |