PHP
  Home arrow PHP arrow Page 3 - Build a Query Processor Class for Networking in PHP 5
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? 
Google.com  
PHP

Build a Query Processor Class for Networking in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-05-17


    Table of Contents:
  • Build a Query Processor Class for Networking in PHP 5
  • Building the class: Converting host names to IP addresses and vice versa
  • Handling Internet services: defining the "getServiceNames()" and "getServicePorts()" methods
  • Assembling the methods: listing the full source code of the "QueryProcessor" class

  • 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


    Build a Query Processor Class for Networking in PHP 5 - Handling Internet services: defining the "getServiceNames()" and "getServicePorts()" methods
    ( Page 3 of 4 )

    As I mentioned before, due to the extensible nature of the "QueryProcessor" class, the next thing that I'll do is add to it a pair of handy methods, aimed at handling the data associated with Internet services and their TCP ports.

    Bearing in mind this concept, the first method, "getServicePorts()", takes an array of predefined Internet services and returns the corresponding TCP port numbers. Its signature is as follows:

    // get TCP ports of Internet services
    public function getServicePorts(){
        $output='Retrieving services ports...Please wait.<br />';
        foreach($this->services as $service){
            if(!$port=getservbyname($service,'tcp')){
                $output.='Error retrieving port of service
    '.$service.'<br />';
            }
            else{
                $output.='Service '.$service. ' runs on TCP port :'.
    $port.'<br />';
            }
        }
        return $output;
    }

    The method listed above shows how to obtain the respective TCP port number of a given Internet service by using the "getservbyname()" PHP built-in function. In this particular case, I decided to display an error message if the TCP port can't be retrieved for some reason, but this condition can be modified and throw an exception instead.

    Also, notice that each Internet service is obtained from the $this->services class property, which means that additionally some of the most common services will be stored as an array inside the constructor. You'll see this when I list the full source code of the "QueryProcessor" class.

    Now that I've explained how the previous method works, take a look at the next one, called "getServiceNames()," which takes an array of TCP ports and returns the corresponding service name, when applicable. The signature of this new method is shown below:

    // get Services by TCP ports
    public function getServiceNames(){
        $output='Retrieving services names...Please wait.<br />';
        foreach($this->ports as $port){
            if(!$service=getservbyport($port,'tcp')){
                $output.='Error retrieving service name on port
    '.$port.'<br />';
            }
            else{
                $output.='TCP Port '.$port. ' is used by service :'.
    $service.'<br />';
            }
        }
        return $output;
    }

    As you can see, the above method can be considered the counterpart of the previous one, since it takes a specified TCP port and attempts to retrieve the service that runs on that port. To perform this conversion process, the method uses internally the "getservbyport()" PHP function, which precisely returns (when possible) the Internet service that corresponds to a given TCP port. Also, you must notice that all the TCP ports are obtained from the $this->ports array, therefore this condition means that TCP ports are assigned as a class property inside the respective constructor.

    At this stage, I added a couple of handy methods to the "QueryProcessor" class, in order to extend its basic functionality. Now, it's time to show its full source code. Therefore, read the next section, in order to get the complete class code.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek