Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

FSocket-Example

Sections:

Example Code

The following code demonstrates how to use the class.

<?php
/*!***********************************************************************
 *************************************************************************
 * \file            fsocket-sample.php
 *
 * \author          Kai Klenovsek
 *
 * \date            First Step: 27.12.2004
 * 
 * \note <br>
 * \b THE \bBEER-WARE \bLICENSE <br>
 *    As long as you retain this notice you can do whatever you want with <br>
 *    this stuff. If we meet some day, and you think this stuff is worth it, <br>
 *    you can buy me a beer in return. <br>
 *
 * \b NOTES: <br>
 *   
***************************************************************************
***************************************************************************/
  
// Include phplibex
require("../../phplibex.inc.php");
    
// Create new socket object
$socket = new fsocket();

unset( $data );
unset( $data_array );

// Makes a socket connection to localhost on port 80 with 30sec. timeout.  
if ( !$socket->fsocket_connect( "localhost", 80, 30 ) )
{
    echo "<b>FSocket error.</b>";
}
else
{
    // Send some data
    $socket->fsocket_send_socketdata( "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" );
       
    /* Get socket data 
     * There are three ways to receive socket data. 
     * 1. If you leave the endsign var free, the function reads only one line. You could also set "LINE" as parameter.
     *    As result you get an data string.
     * 2. If you fill the parameter with "EOF", the function reads the data until "EOF" will be reached.
     *    As result you get an data array.
     * 3. The third option is to set your own break sign or string. 
     *    As result you get an data array. */
    
    // Method 1
    $data = $socket->fsocket_get_socketdata( "EOF" );
    
    // Show the data string
    if ( $data )
        echo "<b>Received data string: </b><br>".htmlentities( $data )."<br><br>";
    
    // Method 2
    // $data_array = $socket->fsocket_get_socketdata( "EOF", 1000 );

    // Method 3
    // $data_array = $socket->fsocket_get_socketdata( ">", 1000 );
    
    if ( $data_array )
    {
        echo "<b>Received data array: </b><br>"; 
        for ( $loop=0; $loop<=$data_array; $loop++ )
        {
            echo $loop." ".htmlentities($data_array[$loop])."<br>";
        }
    }
}

// Close socket connection 
$socket->fsocket_close();


// Socket stream handling
$stream = $socket->fsocket_get_stream();
if ( $stream )
{
    echo "<b>Socket stream:</b><br>";
    for ( $loop=0; $loop <= sizeof($stream); $loop++ )
        echo $stream[$loop]."<br>";
}

// Error message handling
$error = $socket->fsocket_get_errorlist();
 
if ( $error )
{
    echo "<b>Error messages:</b><br>";
    for ( $loop=0; $loop <= sizeof($error); $loop++ )
        echo $error[$loop]."<br>";
}
    
?>

Generated on Fri Aug 26 08:13:54 2005 for PHP-Lib-Xtreme by  doxygen 1.4.3