[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Communications, Volume 2

getservbyname Subroutine


Gets service entry by name.


Standard C Library (libc.a)


#include <netdb.h>

struct servent *getservbyname ( Name, Protocol)
char *Name, *Protocol;


The getservbyname subroutine is threadsafe in AIX 4.3 and later. However, the return value points to static data that is overwritten by subsequent calls. This data must be copied to be saved for use by subsequent calls.

The getservbyname subroutine retrieves an entry from the /etc/services file using the service name as a search key.

An application program can use the getservbyname subroutine to access a service, service aliases, the protocol for the service, and a protocol port number for the service.

The getservbyname subroutine searches the /etc/services file sequentially from the start of the file until it finds one of the following:

Upon locating a matching name and protocol, the getservbyname subroutine returns a pointer to the servent structure, which contains fields for a line of information from the /etc/services file. The netdb.h file defines the servent structure and structure fields.

Use the endservent subroutine to close the /etc/services file.

All applications containing the getservbyname subroutine must be compiled with _BSD set to a specific value. Acceptable values are 43 and 44. In addition, all socket applications must include the BSD libbsd.a library.


Name Specifies the name of a service.
Protocol Specifies a protocol for use with the specified service.

Return Values

The getservbyname subroutine returns a pointer to a servent structure when a successful match occurs. Entries in this structure are in network byte order.

If an error occurs or the end of the file is reached, the getservbyname subroutine returns a null pointer.


/etc/services Contains service names.

Related Information

The endprotoent subroutine, endservent subroutine, getprotobyname subroutine, getprotobynumber subroutine, getprotoent subroutine, getservbyport subroutine, getservent subroutine, setprotoent subroutine, setservent subroutine.

Sockets Overview, and Understanding Network Address Translation in AIX 5L Version 5.2 Communications Programming Concepts.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]