gtpc1m3t | Transmission Control Protocol/Internet Protocol |
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);
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:
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