Operating System - Tru64 Unix
1830210 Members
2107 Online
109999 Solutions
New Discussion

problem in using getservbyname

 
SOLVED
Go to solution
Senthil_Kumar
Occasional Contributor

problem in using getservbyname

Facing a pbm in using the function
'getservbyname'.. (Attached my sample code
below). This code prints correct value only
for 'shell' service. For all others its giving
wrong values. (Doesn’t match with the entry in
/etc/services file). What is wrong here ?
Foll is the entry in /etc/svc.conf file. So
I understand that, the function searches only
in /etc/services file.
services=local

#include
#include
void main(int argc, char *argv[]) {
struct servent *sp = NULL;

sp = getservbyname(argv[1], NULL);
if (sp == NULL) {
(void)fprintf(stderr, "unknown service\n");
return;
}
endservent();
printf("port = %d \n", sp->s_port);
printf("name = %s \n", sp->s_name);
printf("proto = %s \n", sp->s_proto);
}

# ./a.out shell
port = 514
name = shell
proto = tcp
# ./a.out exec
port = 2
name = exec
proto = tcp
# ./a.out klogin
port = 7938
name = klogin
proto = tcp
# ./a.out kshell
port = 8194
name = kshell
proto = tcp
1 REPLY 1
Venkatesh BL
Honored Contributor
Solution

Re: problem in using getservbyname

use "htons(sp->s_port)"