getservbyname() — Get a server entry by name

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

X/Open:
#define _XOPEN_SOURCE_EXTENDED 1
#include <netdb.h>

struct servent *getservbyname(const char *name, const char *proto);
Berkeley sockets:
#define _OE_SOCKETS
#include <netdb.h>

struct servent *getservbyname(char *name, char *proto);

General description

The getservbyname() call searches the /etc/services or tcpip.ETC.SERVICES data set for the first entry that matches the specified service name and protocol name. If proto is NULL, only the service name must match.
Parameter
Description
name
The service name.
proto
The protocol name.

The getservbyname() call returns a pointer to a servent structure for the network service specified on the call. getservbyname(), getservbyport(), and getservent() all use the same static area to return the servent structure. This static area is only valid until the next one of these functions is called on the same thread.

The servent structure is defined in the netdb.h include file and contains the following elements:
Element
Description
s_aliases
An array, terminated with a NULL pointer, of alternative names for the service.
s_name
The official name of the service.
s_port
The port number of the service.
s_proto
The protocol required to contact the service.

Special behavior for C++: To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.

Returned value

The return value points to data that is overwritten by subsequent calls returning the same data structure.

If successful, getservbyname() returns a pointer to a servent structure.

If unsuccessful or End Of File (EOF), getservbyname() returns a NULL pointer.

Related information