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

Technical Reference: Communications, Volume 2

getservbyport Subroutine


Gets service entry by port.


Standard C Library (libc.a)


#include <netdb.h>

struct servent *getservbyport ( Port, Protocol)
int Port;
char *Protocol;


Attention: Do not use the getservbyport subroutine in a multithreaded environment.

The getservbyport subroutine retrieves an entry from the /etc/services file using a port number as a search key.

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

The getservbyport subroutine searches the services file sequentially from the beginning of the file until it finds one of the following:

Upon locating a matching protocol and port number or upon locating a matching protocol only if the Port parameter value equals 0, the getservbyport subroutine returns a pointer to a servent structure, which contains fields for a line of information in 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.


Port Specifies the port where a service resides.
Protocol Specifies a protocol for use with the service.

Return Values

Upon successful completion, the getservbyport subroutine returns a pointer to a servent structure.

Note: The return value points to static data that is overwritten by subsequent calls.

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

Implementation Specifics

The getservbyport subroutine is part of Base Operating System (BOS) Runtime.

All applications containing the getservbyport 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.


                          Contains service names.

Related Information

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

Sockets Overview in AIX 5L Version 5.1 Communications Programming Concepts.

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