Network Helper FunctionsWhen working with network communications, PHP provides a number of useful functions to determine protocol identifiers and port numbers used by specific services. For instance, recall from the previous section the socket_create() function's third parameter: socket_create($domain, $type, $protocol); This $protocol parameter is an integer constant representing the unique identifier given to protocols such as UDP or TCP. In previous examples, when socket_create() was used, the constant SOL_TCP was used for this parameter. Although acceptable for the TCP protocol, for other protocols (such as SMP, simple message protocol) we must use the getprotobyname() function whose syntax is as follows: getprotobyname($name); $name is a string representing the name of the protocol. When executed, this function will attempt to look up the protocol based on the provided name and return its associated integer constant or 1 on error, as shown in Listing 21.10. Listing 21.10. Using getprotobyname()<?php $proto = "smp"; $proto_num = getprotobyname($proto); if($proto_num == -1) { die("Could not find protocol '$proto'\n"); } else { echo "The '$proto' protocol has an ID of $proto_num\n"; } ?> Protocols can also be looked up based on their unique identifiers by using the getprotobynumber() function: getprotobynumber($proto_num); $proto_num is the protocol number to look up. When executed, getprotobynumber()returns a string representing the protocol, or a Boolean false if it was not found as shown in Listing 21.11: Listing 21.11. Using getprotobynumber()<?php $proto_num = SOL_TCP; $proto = getprotobynumber($proto_num); if($proto === false) { die("Could not find protocol with ID $proto_num\n"); } else { echo "The protocol with ID $proto_num is '$proto'\n"; } ?> When working with Network I/O, it is also useful to be able to look up information regarding specific services. For these purposes PHP provides the getservbyname() function: getservbyname($name, $protocol); $name is the name of the service (such as ftp) to look up. To look up a service, a protocol is also specified through the $protocol parameter and must be either the string "udp" or "tcp", respectively. When executed, this function returns the port number associated with the service, or a Boolean false on error, as shown in Listing 21.12: Listing 21.12. Using getservbyname()<?php $service = "ftp"; $port = getservbyname($service, 'tcp'); if($port === false) { die("Lookup of service '$service' failed.\n"); } else { echo "Service '$service' runs on port $port\n"; } ?> Likewise, you can also look up services based on a provided port number using the getservbyport() function: getservbyport($port, $protocol); $port is the port number of the service to look up running on the protocol $protocol. Like getservbyname(), the $protocol parameter represents the protocol under which the service should be looked up and must be either the string "udp" or "tcp". When executed, getservbyport() returns a string representing the service assigned to the provided port, or a Boolean false on failure (see Listing 21.13). Listing 21.13. Using getservbyport()<?php $port = 80; $service = getservbyport($port, 'tcp'); if($service === false) { die("Could not find a service running on $port\n"); } else { echo "The '$service' service runs on port $port\n"; } ?> ![]() |