getprotobyname / getprotobynumber / getservbyname / getservbynumber
getprotobyname(string $protocol): int|false · getprotobynumber(int $number): string|false · getservbyname(string $service, string $protocol): int|false · getservbynumber(int $number, string $protocol): string|false
Protocol/service-name queries: forward and reverse lookups into the /etc/protocols and /etc/services system tables (decades-stable IANA values like tcp=6 and http=80/tcp); unknown names return false. getservbynumber is missing in some PHP builds (see notes).
C C23
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
/* libc ships the same resolvers PHP wraps: getprotobyname/getservbyname (POSIX);
getservbyname's port comes back in NETWORK byte order - ntohs before use! */
int main(void) {
struct protoent *pe = getprotobyname("tcp");
printf("%d\n", pe->p_proto); /* 6 */
struct protoent *pu = getprotobyname("udp");
printf("%d\n", pu->p_proto); /* 17 */
struct servent *se = getservbyname("http", "tcp");
printf("%d\n", ntohs(se->s_port)); /* 80 */
struct servent *sd = getservbyname("domain", "udp");
printf("%d\n", ntohs(sd->s_port)); /* 53 */
printf("missing: %d\n", getservbyname("no-such-svc", "tcp") == NULL);
return 0;
}
6 17 80 53 missing: 1
C++ C++26
#include <arpa/inet.h>
#include <netdb.h>
#include <iostream>
// libc ships the same resolvers PHP wraps: getprotobyname/getservbyname (POSIX);
// the port comes back in NETWORK byte order - ntohs before use!
int main() {
struct protoent *pe = getprotobyname("tcp");
std::cout << pe->p_proto << "\n"; // 6
struct protoent *pu = getprotobyname("udp");
std::cout << pu->p_proto << "\n"; // 17
struct servent *se = getservbyname("http", "tcp");
std::cout << ntohs(se->s_port) << "\n"; // 80
struct servent *sd = getservbyname("domain", "udp");
std::cout << ntohs(sd->s_port) << "\n"; // 53
std::cout << "missing: " << std::boolalpha << (getservbyname("no-such-svc", "tcp") == nullptr) << "\n";
return 0;
}
6 17 80 53 missing: true
C# v11.0.100
using System;
using System.Collections.Generic;
// no name->number resolver: apps embed tables like this (IANA registries behind /etc files)
var ports = new Dictionary<string, int> { ["http"] = 80, ["https"] = 443, ["domain"] = 53 };
var protos = new Dictionary<string, int> { ["tcp"] = 6, ["udp"] = 17 };
Console.WriteLine(protos["tcp"]);
Console.WriteLine(ports["http"]);
Console.WriteLine(ports["domain"]);
Console.WriteLine("missing: " + ports.GetValueOrDefault("no-such", -1));
6 80 53 missing: -1
Go v1.27.4
package main
import (
"fmt"
"net"
)
// net.LookupPort maps service names via the system table; protocol numbers have no
// stdlib lookup and the reverse directions are missing too: hand tables
func main() {
p, _ := net.LookupPort("tcp", "http")
fmt.Println(p) // 80
p2, _ := net.LookupPort("udp", "domain")
fmt.Println(p2) // 53
_, err := net.LookupPort("tcp", "no-such-svc")
fmt.Println("missing:", err != nil)
proto := map[int]string{6: "tcp", 17: "udp"} // hand-maintained: no stdlib proto API
fmt.Println(proto[6])
}
80 53 missing: true tcp
Java v27.0.1
import java.util.Map;
public class Main {
// Java ships no name->number resolver: apps embed tables like this (IANA registries)
static final Map<String, Integer> PORTS = Map.of("http", 80, "https", 443, "domain", 53);
static final Map<String, Integer> PROTOS = Map.of("tcp", 6, "udp", 17);
public static void main(String[] args) {
System.out.println(PROTOS.get("tcp")); // 6: what getprotobyname returns
System.out.println(PORTS.get("http")); // 80: getservbyname's answer
System.out.println(PORTS.get("domain")); // 53
System.out.println("missing: " + PORTS.getOrDefault("no-such", -1));
}
}
6 80 53 missing: -1
JavaScript v26.13.1
// no resolver in the platform: hand tables (values /etc/services has carried for decades)
const PORTS = { http: 80, https: 443, domain: 53 };
const PROTOS = { tcp: 6, udp: 17 };
console.log(PROTOS.tcp); // 6: what getprotobyname returns
console.log(PORTS.http); // 80: getservbyname's answer
console.log(PORTS.domain); // 53
console.log("missing:", PORTS["no-such"] ?? -1);
6 80 53 missing: -1
PHP v8.6.0
// POSIX system tables: /etc/protocols + /etc/services (stable, near-eternal values)
echo getprotobyname("tcp"), "\n"; // 6
echo getprotobynumber(6), "\n"; // "tcp": reverse lookup
echo getprotobyname("udp"), "\n"; // 17
echo getservbyname("http", "tcp"), "\n"; // 80
echo getservbyname("domain", "udp"), "\n"; // 53
echo "missing: ", var_export(getservbyname("no-such-svc", "tcp") === false, true), "\n";
6 tcp 17 80 53 missing: true
Python v3.15.2
import socket
# /etc/protocols + /etc/services live in the stdlib socket module (name -> number only)
print(socket.getprotobyname("tcp")) # 6
print(socket.getprotobyname("udp")) # 17
print(socket.getservbyname("http", "tcp")) # 80
print(socket.getservbyname("domain", "udp")) # 53
try:
socket.getservbyname("no-such-svc", "tcp")
print("missing: False")
except OSError:
print("missing: True")
6 17 80 53 missing: True
Rust v1.99.1
// std has no name->number resolver: embed tables like this (IANA registries behind /etc files)
fn find(table: &[(&str, i32)], name: &str) -> Option<i32> {
table.iter().find(|(k, _)| *k == name).map(|(_, v)| *v)
}
fn main() {
let ports: [(&str, i32); 3] = [("http", 80), ("https", 443), ("domain", 53)];
let protos: [(&str, i32); 2] = [("tcp", 6), ("udp", 17)];
println!("{}", find(&protos, "tcp").unwrap()); // 6: what getprotobyname returns
println!("{}", find(&ports, "http").unwrap()); // 80: getservbyname's answer
println!("{}", find(&ports, "domain").unwrap());
println!("missing: {}", find(&ports, "no-such").unwrap_or(-1));
}
6 80 53 missing: -1
TypeScript v26.13.1
// no resolver in the platform: hand tables (values /etc/services has carried for decades)
const PORTS: Record<string, number> = { http: 80, https: 443, domain: 53 };
const PROTOS: Record<string, number> = { tcp: 6, udp: 17 };
console.log(PROTOS.tcp); // 6: what getprotobyname returns
console.log(PORTS.http); // 80: getservbyname's answer
console.log(PORTS.domain); // 53
console.log("missing:", PORTS["no-such"] ?? -1);
6 80 53 missing: -1
Language differences
The data source is the system tables: /etc/protocols and /etc/services (Windows ships its own services file), endorsed by the IANA registries — tcp=6, udp=17, http=80/tcp, domain=53/udp are stable facts unchanged for decades, so the examples can assert exact values. Deterministic assertions: tcp->6 and http->80 forward lookups, the tcp reverse lookup (getprotobynumber(6)=="tcp"), and unknown names returning false.
Runtime note (honest disclosure): getservbynumber is undefined on this machine's PHP 8.6.0 build (php -r reports Call to undefined function; not caused by disable_functions — a build/compile-surface difference). The manual (PHP 4+) confirms the function exists; cross-build code should guard with function_exists('getservbynumber') — hence the PHP example uses only the three functions verified present here.
Direction coverage: PHP/C/C++ have both directions (the full getprotobyname/getprotobynumber/getservbyname/getservbynumber set); Python's socket module is name-direction only (getprotobyname/getservbyname, no reverse); Go's net.LookupPort maps service names to ports only; Java/C#/JS/TS/Rust ship nothing — the examples hand-write constant tables (values from the same IANA registries).
The big C/C++ trap: getservbyname's s_port is in network byte order (big-endian); printing it raw on x86 yields 20480 (80 byte-swapped) — ntohs is mandatory. Java's relevant classes sit in the private sun surface, not a public API.
Underlying semantics: protocol numbers go into the IP header's protocol field, ports into the TCP/UDP header — two unrelated numbering spaces; getservbyname's protocol parameter ("tcp"/"udp") exists precisely because one service name can register two port sets.