BVBC
BVBC

Reputation: 375

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?

Upvotes: 3

Views: 13391

Answers (4)

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);

Upvotes: 1

Deepak Nayak
Deepak Nayak

Reputation: 51

One good reason why it's returning NULL is because the hostname what you are passing is not correct. Sometimes even doing hostname -v will not give the correct host name.

Try following:

cat /etc/hosts

this will show you output as:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain

That 'localhost' next to 127.0.0.1 in the output above is your host name. This will work perfectly with gethostbyname.

Upvotes: 0

Joshua
Joshua

Reputation: 43327

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.

Upvotes: 1

Mike -- No longer here
Mike -- No longer here

Reputation: 2092

In the linux programmers manual the function has the following declaration:

struct hostent *gethostbyname(const char *name);

This means the parameter must be a char array (or string in layman terms). A quoted string such as "yahoo.com" can be used directly when calling the function.

The following code is a working example on how gethostbyname works:

#include <stdio.h>
#include <string.h>
#include <netdb.h>

int main(){
  struct hostent* h=gethostbyname("yahoo.com");
  printf("Hostname: %s\n", h->h_name);
  printf("Address type #: %d\n", h->h_addrtype);
  printf("Address length: %d\n", h->h_length);

  char text[50]; // allocate 50 bytes (a.k.a. char array)
  strcpy(text,"bing.ca"); //copy string "bing.ca" to first 7 bytes of the array
  h=gethostbyname(text); //plug in the value into the function. text="bing.ca"
  printf("Hostname: %s\n", h->h_name);
  printf("Address type #: %d\n", h->h_addrtype);
  printf("Address length: %d\n", h->h_length);

  return 0;
}

I called it twice. Once for yahoo.com and once for bing.ca and I retrieved the hostname, the address type number and the address length (which is the number of bytes required to store the IP).

For calling the bing address, I allocated a char array, filled it with a string then passed that char array as a parameter to the function.

Upvotes: 0

Related Questions