System_Socket
[ class tree: System_Socket ] [ index: System_Socket ] [ all elements ]

Source for file echo-server.php

Documentation is available at echo-server.php

  1. <?php
  2.  
  3. /**
  4. * Simple TCP echo server (localhost:9876)
  5. * $Id: echo-server.php,v 1.5 2004/04/25 16:31:19 mike Exp $
  6. */
  7.  
  8. require_once 'System/Socket/Creator.php';
  9.  
  10.  
  11. /**
  12. * Create a System_Socket_Listener object with the spcified options passed
  13. * through to the underlying System_Socket.  Used options are typical for
  14. * a TCP/IP listener.
  15. */
  16. $srv &System_Socket_Creator::createListener(
  17.     array(  'port'      => 9876,
  18.             'proto'     => SOL_TCP,
  19.             'domain'    => AF_INET,
  20.             'type'      => SOCK_STREAM,
  21.     )
  22. );
  23.  
  24. // just check if we actually got a System_Socket_Listener
  25. if (PEAR::isError($srv)) {
  26.     die($srv->getMessage());
  27. }
  28.  
  29. // this array will hold already seen connection IDs
  30. $seen = array();
  31.  
  32. /**
  33. * We leave the loop when the listener gets closed
  34. */
  35. while ($srv->hasSocket{
  36.  
  37.     /**
  38.     * Fetch exceptional clients - this should rather never happen, though.
  39.     */
  40.     $excp &$srv->getExceptionalClients();
  41.     while ($conn &$excp->shift()) {
  42.         echo "\nExceptional Client: " $conn->getID("\n";
  43.         $conn->close();
  44.     }
  45.  
  46.     /**
  47.     * We first select writable clients to show a short greeting and usage
  48.     * message.  System_Socket_Listener::getWritableClients() returns a
  49.     * System_Socket_ConnectionPool object like its counterparts for
  50.     * readable and exceptional clients.
  51.     */
  52.     $write &$srv->getWritableClients();
  53.  
  54.     /**
  55.     * Shift one client connection after another and check if we have already
  56.     * seen it before; otherwise display a short usage message.
  57.     */
  58.     while ($conn &$write->shift()) {
  59.         if (!isset($seen[$conn->getID()])) {
  60.             $conn->writeLine();
  61.             $conn->writeLine('Type "exit" to quit or "stop" to stop the server');
  62.             $conn->writeLine();
  63.             $seen[$conn->getID()= true;
  64.         }
  65.     }
  66.     
  67.     /**
  68.     * Now read a line from the clients and write received data back.  If we get
  69.     * a "exit" disconnect the client and if we get a "stop" shutdown the server.
  70.     */
  71.     $read &$srv->getReadableClients();
  72.     while ($conn &$read->shift()) {
  73.         $line $conn->readLine();
  74.         switch (trim($line))
  75.         {
  76.             case 'exit':
  77.                 $conn->close();
  78.             break;
  79.             
  80.             case 'stop':
  81.                 $srv->close();
  82.             break;
  83.             
  84.             default:
  85.                 $conn->write($line);
  86.         }
  87.     }
  88.     usleep(10);
  89. }
  90. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.