Messages in this thread |  | | From | Albert Cahalan <> | Subject | Re: getservbyport | Date | Wed, 15 May 1996 18:28:12 -0400 (EDT) |
| |
>> 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!
I suppose it is _way_ to late to fix, but I have to wonder why getservbyport() doesn't do the conversion itself. A library running on a little-endian machine should not require the programmer to mess with htons() at all.
It almost looks like something a vendor designed to make it difficult to write portable programs. (If you only have a Sun, that's where you test your program for bugs.)
|  |