title: How to test PHP socket TCP/IP Server using Terminal/PHP?
body: <p>I have a server up online, and I made a simple TCP/IP Server using PHP to handle a certain port. The code I use is found below:</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL);
  
  /* Allow the script to wait for connections. */
  set_time_limit(0);
  
  /* Activate the implicit exit dump, so we'll see what we're getting
  * while messages come. */
  ob_implicit_flush();
  
  $address = '123.456.789.123';
  $port = 1234;
  
  if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
      echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  }
  
  if (socket_bind($sock, $address, $port) === false) {
      echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
  }
  
  if (socket_listen($sock, 5) === false) {
      echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
  }
  
  //clients array
  $clients = array();
  
  do {
      $read = array();
      $read[] = $sock;
  
      $read = array_merge($read,$clients);
  
      $write = NULL;
      $except = NULL;
      $tv_sec = 5;
  
      // Set up a blocking call to socket_select
      if(socket_select($read, $write, $except, $tv_sec) &lt; 1)
      {
          //    SocketServer::debug("Problem blocking socket_select?");
          echo "socket continuing";
          continue;
      }
  
      // Handle new Connections
      if (in_array($sock, $read)) {        
  
          if (($msgsock = socket_accept($sock)) === false) {
              echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
              break;
          }
          $clients[] = $msgsock;
          $key = array_keys($clients, $msgsock);
  
          $msg = "\Welcome to the PHP Test Server. \n" .
          "You are the customer number: {$key[0]}\n" .
          "To exit, type 'quit'. To close the server type 'shutdown'.\n";
          socket_write($msgsock, $msg, strlen($msg));
  
      }
  
      // Handle Input
      foreach ($clients as $key =&gt; $client) { // for each client        
          if (in_array($client, $read)) {
              if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
                  echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($client)) . "\n";
                  break 2;
              }
              if (!$buf = trim($buf)) {
                  continue;
              }
              if ($buf == 'quit') {
                  unset($clients[$key]);
                  socket_close($client);
                  break;
              }
              if ($buf == 'shutdown') {
                  socket_close($client);
                  break 2;
              }
              $talkback = "Client {$key}: You said '$buf'.\n";
              socket_write($client, $talkback, strlen($talkback));
              echo "$buf\n";
          }
  
      }        
  } while (true);
  
  socket_close($sock);
  ?&gt;
  </code></pre>
  
  <p>The script just basically runs and allows for more than one connection to run. After uploading the code to the server, I connected to the server, got to the directory where the file is, and then ran <code>php filename.php</code>. It does not show any warning or errors.</p>
  
  <p>However, I need to further configure this TCP/IP Server and do things based on the input it receives. Right now, when I run <code>php filename.php</code>, it doesn't show anything (I'm guessing because all the outputs are written to the socket, and not echoed).</p>
  
  <p>How do I test this TCP/IP Server that I made? <code>telnet</code> is out of the question since it's not secure. Right now I'm looking for nothing too complicated, so the Terminal or another simple PHP File would be a good choice.</p>