Location: PHPKode > scripts > PHP-Lib-Xtreme > php-lib-xtreme/doc/html/fsocket-class.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>PHP-Lib-Xtreme: FSocket-Example</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.3 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1><a class="anchor" name="FSocket-Class">FSocket-Example</a></h1><h2><a class="anchor" name="FSocket_Section">
Sections:</a></h2>
<ul>
<li><a class="el" href="fsocket-class.html#FSocket_ExampleCode">Example Code</a></li></ul>
<h2><a class="anchor" name="FSocket_ExampleCode">
Example Code</a></h2>
The following code demonstrates how to use the class.<p>
<div class="fragment"><pre class="fragment">&lt;?php
<span class="comment">/*!***********************************************************************</span>
<span class="comment"> *************************************************************************</span>
<span class="comment"> * \file            fsocket-sample.php</span>
<span class="comment"> *</span>
<span class="comment"> * \author          Kai Klenovsek</span>
<span class="comment"> *</span>
<span class="comment"> * \date            First Step: 27.12.2004</span>
<span class="comment"> * </span>
<span class="comment"> * \note &lt;br&gt;</span>
<span class="comment"> * \b THE \bBEER-WARE \bLICENSE &lt;br&gt;</span>
<span class="comment"> *    As long as you retain this notice you can do whatever you want with &lt;br&gt;</span>
<span class="comment"> *    this stuff. If we meet some day, and you think this stuff is worth it, &lt;br&gt;</span>
<span class="comment"> *    you can buy me a beer in return. &lt;br&gt;</span>
<span class="comment"> *</span>
<span class="comment"> * \b NOTES: &lt;br&gt;</span>
<span class="comment"> *   </span>
<span class="comment">***************************************************************************</span>
<span class="comment">***************************************************************************/</span>
  
<span class="comment">// Include phplibex</span>
require(<span class="stringliteral">"../../phplibex.inc.php"</span>);
    
<span class="comment">// Create new socket object</span>
$socket = <span class="keyword">new</span> <a class="code" href="classfsocket.html">fsocket</a>();

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

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

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

<span class="comment">// Close socket connection </span>
$socket-&gt;fsocket_close();


<span class="comment">// Socket stream handling</span>
$stream = $socket-&gt;fsocket_get_stream();
<span class="keywordflow">if</span> ( $stream )
{
    echo <span class="stringliteral">"&lt;b&gt;Socket stream:&lt;/b&gt;&lt;br&gt;"</span>;
    <span class="keywordflow">for</span> ( $loop=0; $loop &lt;= <span class="keyword">sizeof</span>($stream); $loop++ )
        echo $stream[$loop].<span class="stringliteral">"&lt;br&gt;"</span>;
}

<span class="comment">// Error message handling</span>
$error = $socket-&gt;fsocket_get_errorlist();
 
<span class="keywordflow">if</span> ( $error )
{
    echo <span class="stringliteral">"&lt;b&gt;Error messages:&lt;/b&gt;&lt;br&gt;"</span>;
    <span class="keywordflow">for</span> ( $loop=0; $loop &lt;= <span class="keyword">sizeof</span>($error); $loop++ )
        echo $error[$loop].<span class="stringliteral">"&lt;br&gt;"</span>;
}
    
?&gt;
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Fri Aug 26 08:13:54 2005 for PHP-Lib-Xtreme by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.3 </small></address>
</body>
</html>
Return current item: PHP-Lib-Xtreme