php中的socket通过持久连接发送接收消息

时间:2016-06-28 14:12:03

标签: php sockets

我有以下情况:

  1. PHP持久套接字服务器使其在无限循环中通过控制台运行
  2. 这是我的server.php并使用'运行一次。 php server.php'命令行命令。

        <?php
        // set some variables
        $host = "127.0.0.1";
        $port = 3000;
        // don't timeout!
        set_time_limit(0);
        // create socket
        $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
        // bind socket to port
        $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
        while(true) {
            // start listening for connections
            $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
            // accept incoming connections
            // spawn another socket to handle communication
            $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
            // read client input
            $input = socket_read($spawn, 1024) or die("Could not read input\n");
            // clean up input string
            $input = trim($input);
            echo "Client Message : ".$input;
            // reverse client input and send back
            $output = strrev($input) . "\n";
            echo socket_send($spawn, $output, strlen ($output), 0) or     die("Could not write output\n");
            // close sockets
         }
         socket_close($spawn);
         socket_close($socket);
        ?>
    
    1. 此文件中的Client.php我向socket服务器发送消息:此时我成功将消息发送到我的套接字服务器。

      <?php
      $host    = "127.0.0.1";
      $port    = 3000;
      $message = "Abhi ye sab kaise karen ahm asdasdasdasd";
      //echo "Message To server :".$message;
      // create socket
      $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
      // connect to server
      $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
      // get server response
      //Send the message to the server
      if( ! socket_send ( $socket , $message , strlen($message) , 0))
      {
             $errorcode = socket_last_error();
             $errormsg = socket_strerror($errorcode);
             die("Could not send data: [$errorcode] $errormsg \n");
      } else {
             echo "Message send successfully \n".$message;
      }
      //socket_close($socket);
      ?>
      
    2. Receiver.php另一个文件,我想接​​收我的套接字服务器正在发送的响应,这是无法正常工作的,并且它一直在加载而没有给出任何输出。

      <?php
      $host    = "127.0.0.1";
      $port    = 3000;
      $message = "Abhi ye sab kaise karen ahm ";
      //echo "Message To server :".$message;
      // create socket
      $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
      // connect to server
      $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
      // get server response
      //Send the message to the server
      while(true){
          $from = '';
          $port = 3000;
          echo socket_recvfrom($socket, $buf, 12, 0, $from, $port);
      
          echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
      }
      //socket_close($socket);
      ?>
      
    3. 我的接收方端没有收到任何输出。我想这样做:

      从客户端发送消息到套接字服务器,一旦套接字服务器发送它我的接收端显示它,我只成功到客户端,我client.php发送消息到server.php但是receiver.php没有收到任何的事情。

      由于

0 个答案:

没有答案