gtpc1m3tTransmission Control Protocol/Internet Protocol

getservbyname -- Get Server Port by Name

The getservbyname function returns the port number for a specified server application name.

Format

#include  <netdb.h>
struct servent *getservbyname(const char *name, const char *proto);

name
The name 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; char name[4] = "FTP"; char proto[4] = "TCP"; int port; appl_name = getservbyname(name, proto); if (!appl_name) printf("unknown application %s\n", name); else { port = appl_name->s_port; printf("getservbyname was successful\n"); }

Related Information