Home arrow PHP arrow Page 3 - Build a Query Processor Class for Networking in PHP 5

Handling Internet services: defining the "getServiceNames()" and "getServicePorts()" methods - PHP

Welcome to the first of three tutorials in a series that covers network programming in PHP. In this article you will learn how to use some useful PHP networking functions that will help you perform a variety of common networking tasks. To that end, you will build a simple networking query application in PHP 5.

TABLE OF CONTENTS:
  1. Build a Query Processor Class for Networking in PHP 5
  2. Building the class: Converting host names to IP addresses and vice versa
  3. Handling Internet services: defining the "getServiceNames()" and "getServicePorts()" methods
  4. Assembling the methods: listing the full source code of the "QueryProcessor" class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 17, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: