We help IT Professionals succeed at work.

Ip Address to host name function

LizardKing
LizardKing asked
on
I have a small program which does reports based on IP Addresses , now I wanted to make these more viewable by adding in the hostname if possible

I know there is a function in VC++ / MFC GetHostbyAddr
but I am wondering if anyone has any examples of using it
or how to do this a better way

Cheers

LK--<
Comment
Watch Question

Commented:
The sockets gethostbyaddress() is the way to do this assuming, of course, there is a RDNS (i.e. Reverse DNS) mapping for the IP.  Note that NOT ALL IP addresses are reverse mapped and so this function will not always return something useful.

What's your hangup with gethostbyaddr() anyway?  To my knowledge the alternative is to query the DNS server directly via lower-level sockets calls.  In other words, a lot of work!

Author

Commented:
Sorry

My main problem was getting the information from the function and getting a working example..

Say I have a CString holding an IP Address taken from a Database.

I then call the gethostbyaddr() function

struct HOSTENT FAR * gethostbyaddr (
  const char FAR * addr,  
  int len,                
  int type                
);

( how :< )

I get a return of HOSTENT how do I get the Name from this

Cheers

LK--<

Commented:
The h_name member of the HOSTENT struct is a pointer to the hostname.

Author

Commented:
I knew that it was in the HOSTENT-> h_name but I still cannot get it out to work with my setup
I'll keep on trying....


Any other examples etc.

Greatly Appreciated

LK--<

Commented:
Please define "cannot get it out to work..."

1) Show your code.
2) Explain what you are getting back.

Author

Commented:
This is the example code I was workin on to get working for me

The trouble I am having is Setting up my CString IPAddress value to use with this code , how do I change it to work with this


/////////////////////////////////
struct sockaddr_in  client;
struct hostent* host;

int sock, fd, len;

...

len = sizeof( client );

fd  = accept( sock, (struct sockaddr*)&client, &len );

if( fd == -1 ) {
    perror( "accept" );
    exit( 1 );
}

host = gethostbyaddr( (const void*)&client.sin_addr,
                      sizeof(struct in_addr),
                      AF_INET );

printf( "Connection from %s: (%s)\n",
        host ? host->h_name : "<unknown>",
        inet_ntoa( client.sin_addr ) );


/////////////////////

I am not compiling / building fully so I am not getting anything back . . .  . . .

Cheers

LK--<

Commented:
>>I am not compiling / building fully so I am not getting anything back .

What does that mean?  How do you know that your code doesn't work if you've not even tried it?

How does CString come into this picture?

I'm confused...(more than usual.)

Author

Commented:
I am stuck on how to get the CString value I have to a
const char FAR * addr,  

So that it will work with the function,

There is no point compiling it if i cannot fill the parameter list .....

I'm also kinda sorking between a few different things here
hence the hazy replies , sorry

I'll try to be more accurate in future...

But do you know where I am coming from now

Cheers

LK--<

Author

Commented:
sorking I mean working :)
Commented:
CString x;
const char *y;

y = (LPCTSTR)x;

See the CString man page for the (LPCSTR) operator.

You do have a copy of the MSDN Library, right?

Author

Commented:
Me Stupido Cheers

LK--<

Explore More ContentExplore courses, solutions, and other research materials related to this topic.