getservbyname, getservbyport |
Prototype: |
#include <netdb.h>
struct servent *getservbyname(const char *name, const char *proto);
struct servent *getservbyport(int port, const char *proto);
|
General Description: |
These functions find the associated servent either by name or by port number. Since a servent can support more than one protocol, you need to supply the protocol as well. |
Return Value: |
The call returns a pointer to servent (defined below) if successful. Otherwise, it returns a NULL.
struct servent {
char *s_name; /* official service name */
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
};
The field p_proto is the protocol number you should use with the service. |
Parameters |
name |
The servent name (e.g., "daytime", "telnet"). |
proto |
Protocol name. This may be any of the recognized protocol names or aliases. |
port |
The port number. |
Possible Errors |
(errno not set) |
|
|
Examples |
#include <netdb.h>
struct servent *s = getservbyname("telnet", "tcp");
printf("%s %d %s ", s->s_name, ntohs(s->s_port), s->s_proto);
s = getservbyport(22, "udp");
printf("%s %d %s ", s->s_name, ntohs(s->s_port), s->s_proto);
(c)Socket Controls
|