`

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

阅读更多
http://bbs.9ria.com/viewthread.php?tid=76152&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000

使用说明:
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();

?>



分享到:
评论

相关推荐

    用php的socket跟flash的socket通信

    ### 使用 PHP 的 Socket 与 Flash 的 Socket 进行通信 #### 一、PHP 的 Socket 编程基础 在本文档中,PHP 的 Socket 编程主要用于实现与 Flash 的 Socket 进行通信。PHP 的 Socket 编程相对较少被提及,但仍然是一...

    Socket与WEB服务器进行通信的资料[收集].pdf

    对于Windows平台,WinINet库提供了一种更高级别的接口来处理HTTP事务,它抽象了Socket通信的细节,使得开发更为简单。然而,直接使用Socket编程虽然较为繁琐,但能更好地理解HTTP协议的工作机制,并且跨平台性更强。...

    flash 提交例子

    标题中的“Flash提交例子”指的是使用Adobe Flash技术进行数据提交的一种示例应用。在Web开发的早期阶段,Flash因其丰富的交互性和跨平台性被广泛用于创建动态内容和交互式应用程序,其中包括用户表单的数据提交。 ...

    flash连接数据库示例

    Adobe Flex框架包含了Flex Data Gateway,它允许Flash Player通过AMF与服务器进行通信。服务器端需要一个支持AMF的网关,如BlazeDS或LCDS,它们可以处理AMF消息并转发到相应的数据源。 五、数据库连接示例 以下是一...

    swf在线录制视频的例子含fla

    3. **数据传输**:使用Flash的Socket或SharedObject类进行数据通信,可能用于将录制的视频发送到服务器。 4. **用户界面(UI)设计**:创建和管理录制控制,如开始、暂停、停止和预览按钮。 5. **视频编码与解码**:...

    flash builder 4.7加载外部swf文件

    综上所述,要在Flash Builder 4.7中加载外部SWF文件,实现点击事件监听以及与后台通信,你需要掌握ActionScript 3.0的Loader类、事件监听和网络通信的相关知识。通过结合这些技术,你可以创建出具有交互性和数据交换...

    flex前台和后台交互例子

    5. **Socket通信** 对于需要低延迟、高吞吐量的应用,Flex还可以使用Socket接口与服务器进行双向通信。这种方式需要服务器端同样支持Socket编程。 6. **事件驱动的通信模型** 在Flex中,前端与后端的交互通常是...

Global site tag (gtag.js) - Google Analytics