Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: Dave Munro <dave@dcs.st-and.ac.uk>
Subject: Re: gethostbyaddr() doesn't find host (Digital Unix V3.2c)
Date: 1996/03/20	[thread overview]
Message-ID: <3150156E.1015@dcs.st-and.ac.uk> (raw)
In-Reply-To: 826554688.24324.0@fersys.demon.co.uk

Your program doesnt work because your inetaddr is not really a string 
but string pointer to exactly 4 characters where each "character" is 
an decimal number between 0 and 255 - in other words the ip number 
"packed" into 32 bits.

Here is your prog. revised to use the inet_addr(3) macro which 
converts your string into an unsigned integer

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

main ()
{
        char inetaddr[16];                      /* internet address of 
source */
        struct hostent *hp;
        unsigned int ip_no ;

        /* addresses changed from real examples.
         * I've tried swapping these, e.g. "1.1.200.160", 
"200.160.1.1");
         */
        sprintf(inetaddr, "138.251.192.40");
        ip_no = inet_addr( inetaddr ) ;

/*        
        hp = gethostbyaddr (inetaddr, 4, AF_INET);
*/
        hp = gethostbyaddr (( char * )&ip_no, 4, AF_INET);

        if (hp == NULL)
        {
                endhostent();
                printf ("Source has unknown Internet address (%s)\n", 
inetaddr);
                exit (1);
        } else printf( "Hostnames is %s\n",hp->h_name ) ;
        
        endhostent();
}


Hope this helps

Dave Munro



      parent reply	other threads:[~1996-03-20  5:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <826554688.24324.0@fersys.demon.co.uk>
1996-03-13  5:00 ` gethostbyaddr() doesn't find host (Digital Unix V3.2c) John Sanders
1996-03-14  5:00 ` Mike Iglesias
1996-03-20  5:00 ` Dave Munro [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox