I am trying to compile the following program on linux
#include<stdio.h>
#include <netdb.h>
int main(){
char *name="localhost";
extern int h_errno;
h_errno = 0;
struct hostent *h;
struct hostent **res;
struct hostent hp_allocated;
char buf[16384];
int buf_len = (sizeof buf) - 1;
int errnop;
errnop = 0;
char ** pch;
h = gethostbyname(name);
if(h_errno == HOST_NOT_FOUND){
printf("HOST NOT FOUND\n");
}
if(h!=NULL){
if (h->h_aliases) {
printf("in this if now\n");
for (pch = h->h_aliases; *pch != NULL; pch++) {
printf("name of alias is %s\n", *pch);
endhostent();
}
printf("end of this if now\n");
}
}
return 0;
}
whenever I try to run, I get the following output,
HOST NOT FOUND
in this if now
name of alias is ip6-localhost
name of alias is ip6-loopback
end of this if now
even though it is populating the hostent structure(i.e h variable) returned by gethostbyname it is still raising host not found error. any suggestions on where this is going wrong or how to fix this?
Is it supposed to behave like this by default. If so how to resolve host not found error by correcting the program?