Re: getservbyport (fwd)

Karl Keyte (kkeyte@esoc.esa.de)
Tue, 14 May 1996 17:15:11 --100


> From owner-linux-kernel-outgoing@vger.rutgers.edu Tue May 14 14:58:03 1996
> Date: Tue, 14 May 1996 02:28:04 +0200 (MET DST)
> From: Bartlomiej Czardybon <czar@dumb.iinf.polsl.gliwice.pl>
> Subject: getservbyport (fwd)
>
> I've got the question: why this produces shit! ?
>
> #include <stdio.h>
> #include <netdb.h>
>
> main() {
> struct servent *serv;
> if ((serv = getservbyport(23, "tcp")) == NULL) {
> printf("shit!\n");
> } else {
> printf("name: %s\n", serv->s_name);
> }
> }

Because you need to put the port number in network format. Replace
your query line with:

if ((serv = getservbyport(htons(23), "tcp")) == NULL) {

No, it doesn't make it clear in the man page!

Regards,

- Karl Keyte -------------------+-------------------------------------
European Space Agency | Private: Erbacher Strasse 6
Robert-Bosch Strasse 5 | D-64283 Darmstadt, Germany
64293 Darmstadt, Germany | Tel: +(49) 6151 22738
Tel: +(49) 6151 902041 | Fax: +(49) 6151 151319
Fax: +(49) 6151 903414 | Mobile: +(49) 172 6723741
e-Mail: kkeyte@esoc.esa.de | e-Mail: karl@koft.rhein-main.de
--------------------------------+-------------------------------------