gethostbyname() function returns empty buffer

BVBC picture BVBC · Nov 17, 2015 · Viewed 10.2k times · Source

I am new to internet programming, and I am trying to use the gethostbyname() function. When I input a string such as "www.yahoo.com" to gethostbyname function it works fine, but when I input a char array, it will always return an empty buffer.

  char hostname[100];
  struct hostent* h;
  gethostname(hostname, sizeof hostname );
  printf("Hostname: %s\n", hostname);
  h = gethostbyname(hostname);

Any idea how to solve this?

Answer

Joshua picture Joshua · Nov 17, 2015

Your server can't resolve itself. The most common way of "fixing" this is to put its own name into its hostfile. While this is a good idea for various reasons, the underlying problem really should be fixed.

  1. The DNS search list should normally be set to the domainname that contains the hostname -or- the hostname should be fully qualified itself.
  2. DNS should be correctly set up for the host.

This makes it not really a C problem at all, but a server configuration problem. Off it goes then.