PHP
  Home arrow PHP arrow Page 4 - Emulating a Basic Web Server with Sockets in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

Emulating a Basic Web Server with Sockets in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-06-05


    Table of Contents:
  • Emulating a Basic Web Server with Sockets in PHP
  • Taking a quick look at the previous TCP server
  • Emulating a Web server
  • Putting the Web server to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Emulating a Basic Web Server with Sockets in PHP - Putting the Web server to work
    ( Page 4 of 4 )

    In order to see how the Web server works, first I’ll define an example “index.htm” web page, which will be saved to the default directory specified in the “WebServer” class. Below, I listed this sample web document:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>TEST WEB DOCUMENT</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    </head>
    <body>
    <h1>THIS IS AN EXAMPLE WEB PAGE</h1>
    </body>
    </html>

    After defining the above (X)HTML file, the Web server will be run by a “webserver.php” file, which obviously will contain the corresponding definition of the “WebServer” class and use an instance of it, like this:

    // instantiate 'WebServer' object
    $webServer=&new WebServer();

    Now that both “index.htm” and “webserver.php” files have been defined respectively, take a look at the following screenshots, which illustrate how to use the Web server by utilizing a Telnet program as the client :

    As shown above, the first screenshot depicts the process where the “index.htm” file is requested and then displayed. The second image shows the response of the server when trying to retrieve a non-existent file, as well as when the server is stopped by entering the STOP command.

    Of course, I think this Web server won’t be capable of beating Apache, but the above example is enough to demonstrate the powerful capabilities of low-level sockets in PHP.

    Final thoughts

    Finally, we’re done. In this new PHP series, you learned how to work with sockets in PHP through the development of different examples, such as creating a TCP server, and more specifically building an application that emulates a basic Web server.

    In all the code samples, I used some of the most common PHP socket-related functions, therefore you shouldn’t have any problems using them as part of your own applications. See you in the next PHP tutorial!   



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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