HomePHP 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.
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.