Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hostent query

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

Im using gethostbyname() to resolve a domain name or IP but i am having trouble trying to return readable data from the member function h_addr_list[0]. Here is a code snippit of what im trying to do ..

struct hostent *he
.
if((he=gethostbyname(host))==NULL) {
exit(-1); /*exit on failue*/
}

my problem is in trying to print the data held in he->h_addr_list[0]. I think im wright in saying that _addr_list[0] is returned in network byte order so no coversion is needed using htonl, i have also tried using inet_ntoa(*((struct in_addr *)he->h_addr_list[0])) with no success.

Can someone please tel me where im going wrong here, and possably point me in the wright direction?

Thanks in advance,
J@yb0t[afro]

Always know what you say, but don't always say what you know!
 
Without seeing more of your code I'm not sure what is going on. Here's a snippet extracted/modified from one of my working programs that should work. Also, inet_ntoa has been deprecated in favor of inet_ntop.

Code:
#include <stdio.h>
#include <netdb.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
  struct hostent *hostPtr = NULL;
  char *remoteHost = NULL;
  char str[INET6_ADDRSTRLEN];
  char **p;

  if (2 != argc)
  {
    fprintf(stderr, "Usage: %s <Host>\n",argv[0]);
    return 1;
  }

  remoteHost = argv[1];

  hostPtr = gethostbyname(remoteHost);
  if (NULL == hostPtr)
  {
    perror("Error resolving server address");
    return 1;
  }

  printf("%s\n",hostPtr->h_name);
  p = hostPtr->h_addr_list;
  for ( ; *p != NULL; p++) {
    printf("address: %s\n",inet_ntop(hostPtr->h_addrtype,p,str,sizeof(str)));
    printf("         %s\n",str);
  }
}

Hope this helps.
 
Thanks itsqsd,

Havent had a chance to modify my code and test this yet, but ill post again when i do and let you know how i get on!

I cant see that i'll have any problems though ..

regards,

J@yb0t[afro]

Always know what you say, but don't always say what you know!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top