I have QNX 4.25L (from the cdrom ‘Product Suite May 2001’) freshly installed.
The problem is gethostbyname() does not read from /etc/hosts file.
THe /etc/resolv.conf already has the line:
lookup file bind
My dns does not have ‘localhost’. Hence, I expect gethostbyname() can
pick up localhost from /etc/hosts. But, it does not. However, ‘telnet’,
‘ping’ can pick up localhost.
A program like this cannot resolve the host name without using dns.
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
int main (int argc, char** argv)
{
struct hostent* p;
p = gethostbyname (argv[1]);
if (p != 0)
{
char* s;
printf (“host=%s\n”, p->h_name);
printf (“length=%d\n”, p->h_length);
s = p->h_addr;
if (s != 0)
{
printf (“h_addr=%d.”, *s++);
printf ("%d.", *s++);
printf ("%d.", *s++);
printf ("%d\n", *s++);
}
}
else
printf (“cannot find %s\n”, argv[1]);
p = gethostbyname (“localhost”);
if (p != 0)
printf (“host=%s\n”, p->h_name);
else
printf (“cannot find localhost\n”);
return 0;
}
$ cc -o a a.c -lsocket -lrpc
/usr/watcom/10.6/bin/wcc386 -zq -ms -4r -i=/usr/watcom/10.6/usr/include -i=/usr/include a.c
/usr/watcom/10.6/bin/wlink op quiet form qnx flat na a op priv=3 op c libp /usr/watcom/10.6/usr/lib:/usr/lib:. f /tmp/a.o l /usr/watcom/10.6/usr/lib/socket3r.lib l /usr/lib/rpc3r.lib op offset=40k op st=32k
$ ./a yahoo.com
host=yahoo.com
length=4
h_addr=216.115.108.245
cannot find localhost
If I remove /etc/resolv.conf (according to documentation, it will use
/etc/hosts),
$ ./a fireopal
cannot find fireopal
cannot find localhost
$ ping fireopal
PING fireopal.empress.com (197.43.220.25): 56 data bytes
64 bytes from 197.43.220.25: icmp_seq=0 ttl=255 time=0 ms
64 bytes from 197.43.220.25: icmp_seq=1 ttl=255 time=0 ms
I wonder if ‘telnet’, ‘ping’, … are linked with different library
from what I have.
Any idea? THanks in advance.
–
Gordon Man
gman@empress.com