PHP
  Home arrow PHP arrow Page 2 - Developing an Extensible TCP Server wi...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Developing an Extensible TCP Server with Sockets in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2006-05-30

    Table of Contents:
  • Developing an Extensible TCP Server with Sockets in PHP
  • A quick look at the previous TCP server
  • Expanding the original TCP server: processing multiple client requests
  • Defining the createSocketServer() function and the SocketServer class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Developing an Extensible TCP Server with Sockets in PHP - A quick look at the previous TCP server


    (Page 2 of 4 )

    Before I show you how to expand the sample TCP server developed in the first tutorial, first allow me list the full source code that creates it, including the procedural script, the "createSocketServer()" function that I defined before, and finally the "SocketServer" class. Here are the corresponding signatures for each case:

    /*
    //**************************************************************
    // procedural script for building a TCP server
    //**************************************************************
    */
    // define  TCP port & local host
    $port=1234;
    $host='127.0.0.1';
    set_time_limit(0);
    // create low level socket
    if(!$socket=socket_create(AF_INET,SOCK_STREAM,0)){
        trigger_error('Error creating new socket',E_USER_ERROR);
    }          
    // tie up socket to TCP port
    if(!socket_bind($socket,$host,$port)){
        trigger_error('Error binding socket to TCP port',E_USER_ERROR);
    }
    // begin listening connections
    if(!socket_listen($socket)){
        trigger_error('Error listening socket
    connections',E_USER_ERROR);
    }
    // create communication socket
    if(!$comSocket=socket_accept($socket)){
        trigger_error('Error creating communication
    socket',E_USER_ERROR);
    }
    // read socket input
    $socketInput=socket_read($comSocket,1024);
    // convert to uppercase socket input
    $socketOutput=strtoupper(trim($socketInput))."n";
    // write data back to socket server
    if(!socket_write($comSocket,$socketOutput,strlen($socketOutput)))
    {
        trigger_error('Error writing socket output',E_USER_ERROR);
    }
    // close sockets
    socket_close($comSocket);
    socket_close($socket);

    */
    //**************************************************************
    // user-defined function for building a TCP server
    //**************************************************************
    /*
    function createSocketServer($host='127.0.0.1',$port=1234){
        if(!preg_match("/^d{1,3}.d{1,3}.d{1,3}.d{1,3}
    $/",$host)){
            trigger_error('Invalid IP address format.',E_USER_ERROR);
        }
        if(!is_int($port)||$port<1||$port>65535){
            trigger_error('Invalid TCP port number.',E_USER_ERROR);
        }
        set_time_limit(0);
        // create low level socket
        if(!$socket=socket_create(AF_INET,SOCK_STREAM,0)){
            trigger_error('Error creating new socket.',E_USER_ERROR);
        }
        // bind socket to TCP port
        if(!socket_bind($socket,$host,$port)){
            trigger_error('Error binding socket to TCP
    port.',E_USER_ERROR);
        }
        // begin listening connections
        if(!socket_listen($socket)){
             trigger_error('Error listening socket
    connections.',E_USER_ERROR);
        }
        // create communication socket
        if(!$comSocket=socket_accept($socket)){
            trigger_error('Error creating communication
    socket.',E_USER_ERROR);
        }
        // read socket input
        $socketInput=socket_read($comSocket,1024);
        // convert to uppercase socket input 
        $socketOutput=strtoupper(trim($socketInput))."n";
        // write data back to socket server
        if(!socket_write($comSocket,$socketOutput,strlen
    ($socketOutput))){
            trigger_error('Error writing socket
    output',E_USER_ERROR);
        }
        // close sockets
        socket_close($comSocket);
        socket_close($socket);
    }

    */
    //**************************************************************
    // PHP class for building a TCP server
    //**************************************************************
    */
    class SocketServer{
        var $host;
        var $port;
        function  SocketServer($host='127.0.0.1',$port=1234){
            if(!preg_match("/^d{1,3}.d{1,3}.d{1,3}.d{1,3}
    $/",$host)){
                trigger_error('Invalid IP address
    format.',E_USER_ERROR);
            }
            if(!is_int($port)||$port<1||$port>65535){
                trigger_error('Invalid TCP port
    number.',E_USER_ERROR);
            }
            $this->host=$host;
            $this->port=$port;
            $this->connect();
        }
        function connect(){
            set_time_limit(0);
            // create low level socket
            if(!$socket=socket_create(AF_INET,SOCK_STREAM,0)){
                trigger_error('Error creating new
    socket.',E_USER_ERROR);
            }
            // bind socket to TCP port
            if(!socket_bind($socket,$this->host,$this->port)){
                trigger_error('Error binding socket to TCP
    port.',E_USER_ERROR);
            }
            // begin listening connections
            if(!socket_listen($socket)){
                trigger_error('Error listening socket
    connections.',E_USER_ERROR);
            }
            // create communication socket
            if(!$comSocket=socket_accept($socket)){
                trigger_error('Error creating communication
    socket.',E_USER_ERROR);
            }
            // read socket input
            $socketInput=socket_read($comSocket,1024);
            // convert to uppercase socket input 
            $socketOutput=strtoupper(trim($socketInput))."n";
            // write data back to socket server
            if(!socket_write($comSocket,$socketOutput,strlen
    ($socketOutput))){
                trigger_error('Error writing socket
    output',E_USER_ERROR);
            }
            // close sockets
            socket_close($comSocket);
            socket_close($socket);
        }
    }

    Right, as you can see above, I listed all the source code corresponding to both procedural and object-oriented approaches, in order to create a sample TCP server. However, as you'll recall, all these examples demonstrate how to build a socket server that processes one request at a time; it is unable to handle multiple petitions.

    Bearing in mind this limitation, in the next section I'll show you how to expand the pertinent TCP server in such a way that it will be able to process multiple client requests.

    To learn how this will be done, please keep on reading.

    More PHP Articles
    More By Alejandro Gervasio


       · Over this second installment, you'll learn how to build a TCP server, which is...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT