gtpc1m3uTransmission Control Protocol/Internet Protocol

getservbyport -- Get Server Name by Port

The getservbyport function returns the server application name based on a specified server port number.

Format

#include  <netdb.h>
struct servent *getservbyport(int port, const char *proto);

port
The port number of the server application.

proto
The protocol of the server application.

Normal Return

This function returns a pointer to a servent structure for the server application specified on the call. The netdb.h header file defines the servent structure, which contains the following elements:

Element
Description

s_name
Official name of the server application.

s_aliases
Null pointer.

s_port
Port number of the server application.

s_proto
Protocol that the server application uses.
Note:
Subsequent getservbyname or getservbyport calls overwrite the data in the servent structure.

Error Return

A NULL pointer indicates an error.

Programming Considerations

Examples

The following example obtains the port associated with a specified server application name.

#include <types.h>
#include <socket.h>
#include <netdb.h>

·
·
·
struct servent *appl_name; int port; char proto[4] = "TCP"; char *name; port = 21; appl_name = getservbyport(port, proto); if (!appl_name) printf("unknown application %s\n", name); else { name = appl_name->s_name; printf("getservbyport was successful\n"); }

Related Information