注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

php开发lamp

《西安--木木》-经历丰富了生活。 架构师QQ群: 246695517

 
 
 

日志

 
 

PHP和FLASH的socket  

2013-05-29 04:54:58|  分类: php应用 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

最近工作闲来无事,于是想到了有不少人为flash的xmlsocket和 php 的socket 多线程通信问题犯愁,经过研究后,测试成功,贴码如下
PHP服务端:

 
<?php
//监听套接字=>数据读取流
    $port = 8653;
$adr = "172.16.100.136";
   
    // create a streaming socket, of type TCP/IP
//创建一个数据流,TCP/IP类型
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   
    // set the option to reuse the port
//设置可以重用端口的选项
    socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
//设置非阻塞
    socket_set_nonblock($sock);
    // "bind" the socket to the address to "localhost", on port $port
    // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
//绑定套接字到指定的地址和端口
//而这意味着所有的消息发送/接受,,断开都要通过这个端口的链接
    socket_bind($sock, 0, $port);
   
    // start listen for connections
//开始侦听链接
    socket_listen($sock);
    // create a list of all the clients that will be connected to us..
    // add the listening socket to this list
//创建一份将连接我们的链接池
//并且将监听套接字也添加到该池子
    $clients = array($sock);
    while (true) {
  // create a copy, so $clients doesn't get modified by socket_select()
  //创建一份复制,防止链接池不会被socket_select( )改变,因为是引用传入
 
  $newsock = @socket_accept($sock);
  if (false === in_array($newsock, $clients) && false !== $newsock) {
   $clients[] = $newsock;
   $data = @socket_read($newsock, 1024, PHP_BINARY_READ);
   $data = trim($data);
   echo $data . "\n";
   if ('<policy-file-request/>' === $data) {
    $msg ="<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>\0";
    socket_write($newsock, $msg, strlen($msg));
    echo "PHP-Auth\n";
   } else {
    socket_getpeername($newsock, $ip);
    echo "New client connected: {$ip}:\n";
    $msg = $data . "\0";
    socket_write($newsock, $msg, strlen($msg));
   }
   continue; 
  } else {
   $read = $clients;
   // get a list of all the clients that have data to be read from
   // if there are no clients with data, go to next iteration
   //获取一份所有能读取到数据的链接池
   //如果传递过来的链接池没有有数据的,进入下一个循环
   $res = @socket_select($read, $write = NULL, $except = NULL, 0);
   if (false === $res) {
    echo "Client disconnected\n";
    break;
   } else if (0 < $res) {
    foreach ($read as $read_sock) {
     if ($sock !== $read_sock) {
      $data = @socket_read($read_sock, 1024, PHP_BINARY_READ);
      if (false === $data) {
       // remove client for $clients array
       $key = array_search($read_sock, $clients, true);
       unset($clients[$key]);
       //var_dump($read_sock);
       echo "client disconnected.\n";
      } else {
       $data = trim($data);
       if ('' !== $data) {
        //var_dump($clients);
        //var_dump($read);
        //echo "resource:$res\n";
        echo $data . "...\n";
        $msg = $data . "\0";
        socket_write($read_sock, $msg, strlen($msg));
       }
      }
     }
    }
   }
  }
    }
    // close the listening socket
//关闭监听套接字
    @socket_close($sock);
?>

HTML代码:

 
< HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html;  charset=GB2312">
<TITLE>3</TITLE>
<script>
function back(msg) {
  var obj = document.getElementById('res');
  obj.innerHTML = obj.innerHTML + msg + "<br>\n";
}
function send() {
  var msg = document.getElementById('msg').value;
  window.document.myFlash.SetVariable("msg", msg);
}
</script>
</HEAD>
<BODY bgcolor="#FFFFFF">
<div id="res"></div>
<!-- URL's used in the movie-->
<!-- text used in the movie-->
< OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="50" HEIGHT="40" id="myFlash" ALIGN="middle">
<PARAM NAME=movie VALUE="3.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<EMBED src="3.swf" quality=high bgcolor=#FFFFFF  WIDTH="550" HEIGHT="400" NAME="myFlash" ALIGN=""
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="sameDomain"   swLiveConnect="true"></EMBED>
</OBJECT>
<input type=text id="msg"><button onclick="send()">Send</button>
</BODY>
</HTML>

FLASH代码:

 
var mySocket = new XMLSocket();
mySocket.connect("172.16.100.136", 8653);
mySocket.onConnect = function(res) {
if (false === res) {
  getURL("javascript:back('Failed.')");
} else {
  getURL("javascript:back('success.')");
}
};
mySocket.onData = function(msg) {
getURL("javascript:back('Recv:" + msg + "')");
return false;
};
mySocket.onClose = function() {
getURL("javascript:back('Close!')");
};
function sendmsg(msg){
mySocket.send(msg);
getURL("javascript:back('send:" +  msg + "')");
return false;
};
addProperty("msg",function(){},sendmsg);


  评论这张
 
阅读(139)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017