LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2001, 12:17 PM   #1
jmdey
LQ Newbie
 
Registered: Jan 2001
Location: india
Posts: 9

Rep: Reputation: 0
help gethostbyaddress


whats wrong here
//dont worry the header files hav been included

main()
{
struct sockaddr_in sad;

struct hostent *p;

sad.family=AF_INET ;
sad.sin_addr.s_addr = inet_addr("191.34.76.9");
sad.sin_port=htons(6000);

p=gethostbyaddr((char *)&sad,sizeof(sad),AF_INET);

printf("%u",p); //here i get a null always! and thts the problem!!!

}
 
Old 06-28-2001, 01:41 PM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
I think you'd be better with this post in the programming forum! Ask one of the moderators (or Jeremy) to move it for you... Try to avoid just posting it again though as cross posts can be a pain.

Jamie...
 
Old 06-28-2001, 10:49 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You are passing the wrong struct into gethostbyaddr. You were passing a sockaddr_in when you need to pass a in_addr struct. Take a look at the following exampe program:

Code:
#include <netdb.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <errno.h>

main( int argc, char *argv[])
{
   struct in_addr addr;
   struct hostent *hp;
   char *ptrError = NULL;

   if ( argc != 2 )
   {
      printf("Usage: %s ip_address\n\n", argv[0]);
      exit(0);
   }
   addr.s_addr = inet_addr( argv[1] );
   if (addr.s_addr == -1)
   {
      printf("Bad address\n\n");
      exit(-1);
   }
   hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
   if (!hp)
   {
      printf("[%d] %s\n", errno, strerror( errno ) );
      herror( ptrError );
      printf( "%s\n", ptrError );
      exit(-1);
   }
   else
      printf( "Hostname: %s\n\n", hp->h_name);
}
A simple change to your program would make it work:

Code:
main()
{
   struct sockaddr_in sad;
   struct hostent *p;

   sad.sin_addr.s_addr = inet_addr( "198.186.203.77" );

   p = gethostbyaddr((char *)&(sad.sin_addr),sizeof(sad.sin_addr),AF_INET);

   printf("%u",p); //here i get a null always! thats the problem!!!

}
Notice that I pass in &sad.sin_addr into gethostbyaddr.

Last edited by crabboy; 06-28-2001 at 10:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration