PHP Classes

File: simple.php

Recommend this page to a friend!
  Classes of Joshua hatfield   floSocket   simple.php   Download  
File: simple.php
Role: Example script
Content type: text/plain
Description: A simple TCP example script.
Class: floSocket
Implement TCP socket server scripts
Author: By
Last change: Missing quote.
Date: 17 years ago
Size: 5,090 bytes
 

Contents

Class file image Download
#!/usr/local/bin/php -q
<?
/***************************************************************************
 * Original floSocket copyright (C) 2005 by Joshua Hatfield. *
 * *
 * In order to use any part of this floSocket Class, you must comply with *
 * the license in 'license.doc'. In particular, you may not remove this *
 * copyright notice. *
 * *
 * Much time and thought has gone into this software and you are *
 * benefitting. We hope that you share your changes too. What goes *
 * around, comes around. *
 ***************************************************************************

This is a sample script showing how to use floSocket. You may need to change
the "shebang" at the top (#!/.../php -q) to refer to your installation of PHP.
Please refer to floSocket.php for requirement specifications.
*/

// Next, we need to include the class file.
include("floSocket.php");

// Then, specify on what IP address and ports to listen for use later in
// initializing the class. It is easier to do this in one section reserved
// for global script settings.
$serveraddress = false;
$serverport = "1234";

// It's not a bad idea to initialize the class early so you don't try to
// referrence it before it's created accidentily.
$mySocket = new floSocket($serveraddress, $serverport);

// We don't want this to timeout on us, we want to use our own timeout system.
// For this example, 60 seconds should be sufficient.
set_time_limit(0);
$mySocket->set_timeout(60);
/*
You may also want to set the max_execution_time directive in your php.ini file
if you are running in safe mode and you want to run your script indefinitely.

Additionally, your webserver can have other timeouts. E.g. Apache has Timeout
directive, IIS has CGI timeout function, both default to 300 seconds. See the
webserver documentation for meaning of it.
*/

// Let's assign some event capturing functions.
// In the event fired by a new connection, one variable is passed (in this case
// named $channel) which contains an integer index of the floSocket channel
// referrence.
function fun_new_connection($channel) {
   
// We have to reference $mySocket because it is not local in this
    // function and we are planning on using it in this example.
   
global $mySocket;
   
   
// Like I said, we are going to use it. get_remote_address and
    // get_remote_port are pretty straight forward functions.
   
$remote_address = $mySocket->get_remote_address($channel);
   
$report_port = $mySocket->get_remote_port($channel);
   
   
// Simple echo lets us know at our console someone connected.
   
echo("New connection ($channel) from $remote_address on port $report_port\n");
}
// The event fired by new data passes the channel as well as the current buffer
// one byte at a time.
function fun_data_received($channel, $buffer) {
   
// Again, I want to use the socket class in this function.
   
global $mySocket;

   
// You may want to write a simple buffer compiling script for however
    // you want the information treated. In this example, I'm just spitting
    // it back to the console one character at a time.
   
echo("New data ($channel): $buffer\n");
   
   
// Oh heck, why not spit it back to the peer so he can see what he's
    // typing...
   
$mySocket->write($channel, $buffer);
   
   
// A simple if statement can stop the server on a keystroke. In this
    // case, the Esc (escape) key. And of course, tell the peers what happened.
   
$escape_character = chr(27);
    if (
$buffer == $escape_character) {
        echo(
"STOPPING SERVER!!! ESC DETECTED");
       
$mySocket->stop("Server stopped by ESC detection.");
    }

   
// Or, you can just close the one connection, here with "q" (or "Q").
   
if ($buffer == "q" || $buffer == "Q") $mySocket->close($channel, "Channel closed by Q detection.");
}
// Each time a connection is lost, whether it was intentionally closed by
// floSocket or just detected here, the connection expire event will fire.
// If an error was detected in floSocket or passed through the closure
// functions, it will be passed to here along with the channel. If the closure
// was called through floSocket, the event fires BEFORE the connection is
// closed so you can send any remaining information.
function fun_connection_lost($channel, $error) {
    echo(
"Connection lost ($channel): $error\n");
}

// One last thing, we need to tell floSocket what functions are for what events.
$mySocket->set_handler(SOCKET_EVENT_CREATED, "fun_new_connection");
$mySocket->set_handler(SOCKET_EVENT_NEWDATA, "fun_data_received");
$mySocket->set_handler(SOCKET_EVENT_EXPIREY, "fun_connection_lost");

echo(
"starting\n");
// Now that we've got our events set up, let's start the server.
$mySocket->start();
echo(
"finished\n");
?>