php进程间通信

  1. 安装、启用扩展
     php-sockets
    

单客户端1对1通信

  1. 单客户端1对1通信服务端代码
     <?php
     //连接超时设为永不
     set_time_limit(0);
     //声明要绑定的IP和端口
     $port = 1437;
     $ip = '0.0.0.0';
     //创建套接字通信
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if ($socket < 0) {
         echo "创建套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //绑定套接字通信
     $bind = socket_bind($socket, $ip, $port);
     if ($bind < 0) {
         echo "绑定套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //监听套接字通信
     $listen = socket_listen($socket, 4);
     if ($listen < 0) {
         echo "监听套接字通信失败" . PHP_EOL;
         exit(0);
     }
     do {
         //接收套接字通信
         $socket_msg = socket_accept($socket);
         if ($socket_msg < 0) {
             echo "接收套接字通信失败" . PHP_EOL;
             exit(0);
         }
         //读取数据
         $str_r = socket_read($socket_msg, 8192, PHP_BINARY_READ );
         //回复数据
         $str_w = "回复:{$str_r}" . PHP_EOL;
         //回复套接字通信
         if(!socket_write($socket_msg, $str_w, strlen($str_w))){
             echo "回复套接字通信失败" . PHP_EOL;
             exit(0);
         }
     } while (true);
     //关闭套接字通信
     //socket_close($socket);
    

客户端代码

  1. 客户端PHP代码
     <?php
     //连接超时设为永不
     set_time_limit(0);
     //声明要绑定的IP和端口
     $port = 1437;
     $ip = "127.0.0.1";
     //创建套接字通信
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if ($socket < 0) {
         echo "创建套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //连接套接字通信
     $conn = socket_connect($socket, $ip, $port);
     if ($conn < 0) {
         echo "连接套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //CLI交互取得发送数据
     fwrite(STDOUT, "发送:");
     $str_w = trim(fgets(STDIN));
     //发送套接字通信
     if (!socket_write($socket, $str_w, strlen($str_w))) {
         echo "发送套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //接收套接字通信
     $str_r = socket_read($socket, 8192, PHP_BINARY_READ);
     echo $str_r;
     //关闭套接字通信
     socket_close($socket);
    

多客户端n对n通信(暂时不要采用)

  1. 多客户端n对n通信服务端代码
     <?php
     //连接超时设为永不
     set_time_limit(0);
     //声明要绑定的IP和端口
     $port = 1437;
     $ip = '0.0.0.0';
     //创建套接字通信
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if ($socket < 0) {
         echo "创建套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //绑定套接字通信
     $bind = socket_bind($socket, $ip, $port);
     if ($bind < 0) {
         echo "绑定套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //监听套接字通信
     $listen = socket_listen($socket, 4);
     if ($listen < 0) {
         echo "监听套接字通信失败" . PHP_EOL;
         exit(0);
     }
     //客户端句柄集合↓(多客户端)
     $clients = [];
     //客户端句柄集合↑(多客户端)
     do {
         //客户端句柄集合↓(多客户端)
         //接收客户端句柄
         $client = socket_accept($socket);
         if ($client < 0) {
             echo "接收客户端句柄失败" . PHP_EOL;
         } else {
             if(!in_array($client, $clients)){
                 $clients[] = $client;
             }
         }
         //客户端句柄集合↑(多客户端)
         //读取数据
         $str_r = socket_read($client, 8192, PHP_BINARY_READ );
         //回复数据
         $str_w = "回复:{$str_r}" . PHP_EOL;
         //回复套接字通信↓(多客户端)
         foreach ($clients as &$client) {
             if (!socket_write($client, $str_w, strlen($str_w))) {
                 //如果客户端连不上,删除客户端句柄
                 unset($client);
             }
         }
         //回复套接字通信↑(多客户端)
     } while (true);
     //关闭套接字通信
     //socket_close($socket);
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""