.TH getservbyname() "" "" "Sockets Function (libsocket)"
.PC "Get a service entry by name"
.B "#include <netdb.h>"
\fBstruct servent *getservbyname(\fIname\^\fB, \fIprotocol\^);\fR
\fBchar *\fIname\^\fB, *\fIprotocol\^\fB;\fR
.PP
Function
.B getservbyname()
searches file
.BR /etc/services ,
which describes the services offered by TCP/IP on your local network,
for the services offered by
.IR name .
If
.I protocol
is not NULL, the search must also match the protocol it names.
.B /etc/services
must first have been opened by a call to
.BR setservent() .
.PP
.B getservbyname()
returns a pointer to a structure of type
.BR servent ,
which is defined in header file
.BR <netdb.h> :
.DM
struct servent {
	char *s_name;	/* official name of service */
	char **s_aliases;	/* alias list */
	int s_port;	/* port service resides at */
	char *s_proto;	/* protocol to use */
};
.DE
.PP
The following details each member:
.IP \fBs_name\fR
The official name of the service.
.IP \fBs_aliases\fR
This points to a zero-terminated list of alternate names for the service.
.IP \fBs_port\fR
The port number at which the service resides.
Port numbers are returned in network byte order.
.IP \fBs_proto\fR
The name of the protocol to use when contacting the service.
.PP
To close
.BR /etc/services ,
call function
.BR endservent() .
.PP
.B getservbyname()
returns NULL if an error occurs, or if it encounters the end of the file.
.SH "See Also"
.Xr "endservent()," endserve
.Xr "getservent()," getserven
.Xr "getservbyport()," getservbp
.Xr "libsocket," libsocket
.Xr "netdb.h," netdb.h
.Xr "setservent()" setserven
.SH Notes
This function uses a static data space.
If your application needs to save these data, it must copy them
before any subsequent calls overwrite them.
