Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Converting service names into port numbers and vice versa

&pagelevel(3)&pagelevel

A service is expected to be reachable on a specific port and use just one communications protocol. This view is consistent within the Internet domain but does not apply in some other networks. A service may also be reachable on several ports, in which case higher level library functions have to be forwarded or extended.

The getservbyname() function converts a service name into a port number. The service name and, optionally, the name of a qualifying protocol are passed when getservbyname() is called.

The getservbyport() function converts a port number into a service name. The port number and, optionally, the name of a qualifying protocol are passed when getservbyport() is called.

The functions getservbyname() and getservbyport() return a pointer to an object of data type struct servent as their result.

The servent structure is declared as follows:

struct servent {
     char *s_name;          /* Official name of the service */
     char **s_aliases;      /* Alias list */
     int  s_port;           /* Number of the port on which the service lies*/
     char *s_proto;         /* Protocol used */
};

Example

The following program code returns the port number of the telnet service which uses the TCP protocol:

struct servent *sp;
...
sp = getservbyname("telnet", "tcp");