Current Location: Home> Latest Articles> How to combine getservbyname and getprotobyname to query protocols and ports

How to combine getservbyname and getprotobyname to query protocols and ports

gitbox 2025-05-31

Function introduction

getservbyname()

getservbyname(string $service, string $protocol): int|false

This function is used to query the corresponding port number through the service name and protocol name. For example, HTTP services typically use the TCP protocol and listen to port 80.

 $port = getservbyname('http', 'tcp');
echo $port; // Output: 80

getprotobyname()

getprotobyname(string $name): int|false

This function is used to obtain the protocol number according to the protocol name (such as "tcp", "udp"). For example:

 $proto = getprotobyname('tcp');
echo $proto; // Output: 6

Combined use scenarios

When we need to connect to a certain network service, we often need not only know the port number of the service, but also the protocol number used. For example, when establishing a connection using socket_create() and socket_connect() , you need to provide a protocol number as one of the parameters.

Using two functions together can be operated as follows:

 $service = 'http';
$protocol = 'tcp';

$port = getservbyname($service, $protocol);
$proto = getprotobyname($protocol);

if ($port === false || $proto === false) {
    die("Unable to resolve service or agreement\n");
}

echo "Serve '{$service}' Use port: {$port}\n";
echo "protocol '{$protocol}' The number is: {$proto}\n";

Practical application example: Create a TCP socket

Here is a simple example of using these two functions to create a TCP connection:

 $service = 'http';
$protocol = 'tcp';
$host = 'gitbox.net';

$port = getservbyname($service, $protocol);
$proto = getprotobyname($protocol);

if ($port === false || $proto === false) {
    die("Serve或protocol无法识别\n");
}

$socket = socket_create(AF_INET, SOCK_STREAM, $proto);
if ($socket === false) {
    die("Unable to create socket: " . socket_strerror(socket_last_error()) . "\n");
}

$result = socket_connect($socket, $host, $port);
if ($result === false) {
    die("Unable to connect to {$host}:{$port} - " . socket_strerror(socket_last_error($socket)) . "\n");
}

echo "Connected to successfully {$host}:{$port},使用protocol编号 {$proto}\n";
socket_close($socket);

In this example, we use getservbyname() to get the HTTP corresponding port 80, getprotobyname() to get the TCP protocol number 6, and then create and connect a TCP socket to port 80 of gitbox.net .


Things to note

  1. These two functions rely on the system's service and protocol definition files, such as /etc/services and /etc/protocols , which may cause parsing failure if the definition is incomplete.

  2. The result of using these functions should always be judged incorrectly to avoid runtime errors.

  3. These functions are simple encapsulations for getservbyname and getprotobyname in C language, and are very friendly to users who are familiar with underlying socket programming.