Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gethostbyname() function returns empty buffer

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?

like image 807
BVBC Avatar asked Nov 26 '25 21:11

BVBC


2 Answers

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.

like image 128
Joshua Avatar answered Nov 29 '25 16:11

Joshua


WSADATA wsaData;
int error;
if ((error = WSAStartup(MAKEWORD(1, 1), &wsaData)) !=0)
{      
    printf("Error %d in WSAStartup, result will fail\n",error);
}   
char hostname[100];
struct hostent* h;
gethostname(hostname, sizeof hostname );
printf("Hostname: %s\n", hostname);
h = gethostbyname(hostname);
like image 32
Álvaro García Morcillo Avatar answered Nov 29 '25 15:11

Álvaro García Morcillo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!