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

Technical Reference: Communications, Volume 2

getservbyport_r Subroutine


Gets service entry by port.


Standard C Library (libc.a)


#include <netdb.h>

int getservbyport_r(Port, Proto, servent, serv_data)
int Port;
const char *Proto;
struct servent *servent;
struct servent_data *serv_data;


The getservbyport_r subroutine retrieves an entry from the /etc/services file using a port number as a search key. An application program can use the getservbyport_r subroutine to access a service, service aliases, the protocol for the service, and a protocol port number for the service.

The getservbyport_r 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 where the Port parameter value equals 0, the getservbyport_r 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, the servert_data structure, and their fields.

The getservbyport routine overwrites static data returned on subsequent calls. The getservbyport_r routine does not.

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


Port Specifies the port where a service resides.
Proto Specifies a protocol for use with the service.
servent Points to the servent structure.
serv_data Points to the serv_data structure.

Return Values

The function returns a 0 if successful and a -1 if unsuccessful.

If an error occurs or the end of the file is reached, the getservbyport_r subroutine returns a -1 to indicate error.


/etc/services Contains service names.

Related Information

endservent_r Subroutine, setservent_r Subroutine, getservent_r Subroutine, and getservbyname_r Subroutine.

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