简体   繁体   English

php socket server 检查客户端是否连接

[英]php socket server check client is connected or not

this socket server is working fine but dont understand how to check if client is still connected if not close conection can any body help to check if client is still connected if no how to close that connection这个套接字服务器工作正常,但不明白如何检查客户端是否仍然连接,如果没有关闭连接,任何机构都可以帮助检查客户端是否仍然连接,如果没有如何关闭该连接

thanks谢谢

<?php
  // PHP SOCKET SERVER

 // Configuration variables
  $host = "127.0.0.1";
  $port = 3000;
  $max = 5000;
  $client = array();

  // No timeouts, flush content immediatly
  @set_time_limit(0);
  ob_implicit_flush();

  // Server functions
    // Create socket
  $sock = @socket_create(AF_INET, SOCK_STREAM, 0) or die("[" . date('Y-m-d H:i:s') . "] Could not create socket\n");
  // Bind to socket
  socket_bind($sock, $host, $port) or die("[" . date('Y-m-d H:i:s') . "] Could not bind to socket\n");
  // Start listening
  socket_listen($sock) or die("[" . date('Y-m-d H:i:s') . "] Could not set up socket listener\n");

  rLog("Server started at " . $host . ":" . $port);
  // Server loop
  while (true) {
      socket_set_block($sock);
      // Setup clients listen socket for reading
      $read[0] = $sock;
      for ($i = 0; $i < $max; $i++) {
          if (@$client[$i]['sock'] != null)
              $read[$i + 1] = $client[$i]['sock'];
      }
      // Set up a blocking call to socket_select()
      $ready = socket_select($read, $write = null, $except = null, $tv_sec = null);
      // If a new connection is being made add it to the clients array
      if (in_array($sock, $read)) {
          for ($i = 0; $i < $max; $i++) {
              if (@$client[$i]['sock'] == null) {
                  if (($client[$i]['sock'] = socket_accept($sock)) < 0) {
                      rLog("socket_accept() failed: " . socket_strerror($client[$i]['sock']));
                  } else {

                      rLog("Client #" . $i . " connected");
                  }
                  break;
              } elseif ($i == $max - 1) {
                  rLog("Too many clients");
              }
          }
          if (--$ready <= 0)
              continue;
      }
      for ($i = 0; $i < $max; $i++) {
          if (in_array(@$client[$i]['sock'], $read)) {
              $input = socket_read($client[$i]['sock'], 1024);
              $n = trim($input);
              if ($input) {
                  // Strip whitespaces and write back to user
                  // Respond to commands
                  /*$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
                   socket_write($client[$i]['sock'],$output);*/

                   $processRe = processRequest($input);
                    sendMessage($client[$i]['sock'],$processRe);

              }
          } else {

              //if($client[$i]['sock']!=null){
              // Close the socket
              //socket_close($client[$i]['sock']);
              //unset($client[$i]);
              //rLog("Disconnected(1) client #".$i);
              //}
          }
      }
  }
 socket_close($sock);
?>

When client close connection, then socket_select funcion will be return and specified socket handle to client will be available for socket_read when you call socket_read you will get FALSE , this mean client closed connection.当客户端关闭连接时,将返回socket_select函数,并且当您调用socket_read时, socket_read可以使用指定的客户端套接字句柄,您将获得FALSE ,这意味着客户端关闭连接。

But when client is connected, you can close connection by caling socket_close function on his client socket handle.但是当客户端连接时,您可以通过在他的客户端套接字句柄上socket_close function 来关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM