Two PHP functions, getservbyname() and getservbyport(),
are available for learning more about services and their corresponding port numbers.
Retrieving a Service??™s Port Number
The getservbyname() function returns the port number of a specified service. Its
prototype follows:
int getservbyname(string service, string protocol)
408 CHAPTER 16 ?– NE TWORKING
The service corresponding to service must be specified using the same name as
that found in the /etc/services file. The protocol parameter specifies whether
you??™re referring to the tcp or udp component of this service. Consider an example:
echo "HTTP's default port number is: ".getservbyname("http", "tcp");
?>
This returns the following:
HTTP's default port number is: 80
Retrieving a Port Number??™s Service Name
The getservbyport() function returns the name of the service corresponding to the
supplied port number. Its prototype follows:
string getservbyport(int port, string protocol)
The protocol parameter specifies whether you??™re referring to the tcp or the udp
component of the service. Consider an example:
echo "Port 80's default service is: ".getservbyport(80, "tcp");
?>
This returns the following:
Port 80's default service is: http
Establishing Socket Connections
In today??™s networked environment, you??™ll often want to query services, both local and
remote. Often this is done by establishing a socket connection with that service.
Pages:
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489