PHP Classes

File: echo_server.php

Recommend this page to a friend!
  Classes of MC Breit   Simple Server   echo_server.php   Download  
File: echo_server.php
Role: Example script
Content type: text/plain
Description: A Simple ECHO Server, acts interactive to the clients input - Demonstrates the "connection keep alive" routine..
Class: Simple Server
Implement TCP socket servers
Author: By
Last change:
Date: 17 years ago
Size: 3,791 bytes
 

Contents

Class file image Download
<?php
/***************************************************************************
 * SimpleServer Example 1
 * ----------------------
 * begin : Saturday, Dec 4, 2004 - 13:30
 * copyright : (C) 2004 MC Breit
 * email : support@mcb.cc - MCB.CC - Free and Open Sources
 * last modified : 04/12/04 - 23:44 - MC Breit
 * version : 1.0.0
 *
 ***************************************************************************/

/***************************************************************************
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 ***************************************************************************/

/***************************************************************************
 *
 * This example will show how easy to create your own ECHO Server.
 *
 * What should out ECHO Server do?
 * Our ECHO Server should listen to port 8000 on the local ip address
 * 127.0.0.1 and waiting for clients conntcting in.
 * On connect of a client it should send a welcome message.
 * While a client is connected, it should wait that the client is
 * sending a "\r\n" and the return the message the cleint sended before.
 * If the client entered "quit" it should close the connection to the
 * client and say "Bye bye..", the same when reciving "shutdown", but
 * then we would completely shutdown the Server.
 *
 * To test the server, you can run it using the console and run telnet
 * in an other console window.
 * Then connect to "127.0.0.1 port 8000" with telnet and type anything.
 * For example under Windows you can exec "cmd" and then type in:
 * C:\> telnet 127.0.0.1 8000
 * After that you should see the welcome message.
 *
 * !! Important: Dont forget your firewall settings while testing !!
 ***************************************************************************/

//Kill the max_ex_time (on pure installations not needed)
set_time_limit(0);
 
//Including the main class..
include('SimpleServer.class.php');

//Creating a new server object using port 8000 and the local ip "127.0.0.1".
$srv = &new SimpleServer(8000, '127.0.0.1');
//Setting the divider of the messages to \r\n.
$srv->set_msg_divider("\r\n");

//Main loop to listen for clients..
while( $srv->listen() )
{
 
//Say hello to the client..
 
$srv->put("Welcome to our PHP-Echo Server!\r\n");
 
$srv->put("Your IP: ".$srv->remote_ip()."\r\n");
 
$srv->put("Enter 'quit' to close the connection,\r\n");
 
$srv->put("or 'shutdown' to shutdown the server.\r\n");

 
//Working loop - Works with the client inside..
 
while( $srv->is_connected )
  {
   
//Waiting for input from client..
   
$str = $srv->get();
   
//What to do with the input?
   
switch($str)
    {
      case
'quit':
       
//Would he quit? Then kick him off!
       
$srv->put("Bye bye..\r\n");
       
$srv->close();
        break;
      case
'shutdown':
       
//Would he shutdown thois Server? Then do so!
       
$srv->put("Shuting down..\r\n");
       
$srv->shutdown();
        break;
      default:
       
//Nothing? Ok, than we will ECHO back your string..
       
$srv->put($str."\r\n");
        break;
    }
  }
 
 
//Any errors inside this loop?
 
if( $error = $srv->get_error_str() )
  {
    print
$error;
  }
}

//Any errors while shutting down?
if( $error = $srv->get_error_str() )
{
  print
$error;
}

//
// Thats it folks!
//
?>