Home > Archive > Unix Programming > December 2004 > Get host name from IP address
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Get host name from IP address
|
|
| Christian 2004-12-28, 9:12 am |
| Hello guys,
I'm trying to write a simple function which would return the FQDN of a host
from its IPaddress :
int main( int argc, char *argv[] ) {
int len;
char answer[4096];
if( res_init( ) != 0 )
return -1;
len = res_query( argv[1], C_IN, T_A, answer, 4096 );
printf( "len = %d\nerr = %d\n", len, h_errno);
return 0;
}
But if argv[1] is an IP address, res_query returns -1 with h_errno = 1
(Operation not permitted).
If argv[1] is, say, www.yahoo.com, no errors occurs.
Any idea of what's wrong ?
Thanks
Christian
| |
| DINH Viet Hoa 2004-12-28, 9:12 am |
| Christian wrote :
> But if argv[1] is an IP address, res_query returns -1 with h_errno = 1
> (Operation not permitted).
Why not use gethostbyname() ?
You'll get a hostent structure that will contain the host name.
--
DINH V. Hoa,
"tuning et dope, aucun rapport" -- b.
| |
| Christian 2004-12-28, 9:12 am |
|
"DINH Viet Hoa" <dinh.viet.hoa@free.fr> a écrit dans le message de
news:etPan.41d14058.34c9249f.3d6d@utopia...
> Christian wrote :
>
>
> Why not use gethostbyname() ?
>
> You'll get a hostent structure that will contain the host name.
>
> --
> DINH V. Hoa,
>
> "tuning et dope, aucun rapport" -- b.
>
gethostbyname() with www.yahoo.fr gives :
h_name = www.euro.yahoo.akadns.net
h_aliases = www.yahoo.fr
but with 193.108.197.230, it gives :
h_name = 193.108.197.230
i want a function that would return h230.cornut.fr from 193.108.197.230.
Any other idea ?
| |
| Bjorn Reese 2004-12-28, 9:12 am |
| Christian wrote:
> i want a function that would return h230.cornut.fr from 193.108.197.230.
>
> Any other idea ?
gethostbyaddr()
--
mail1dotstofanetdotdk
| |
| Christian 2004-12-28, 9:12 am |
|
"Bjorn Reese" <breese@see.signature> a écrit dans le message de
news:41d1512b$0$671$ba624c82@nntp02.dk.telia.net...
> Christian wrote:
>
>
> gethostbyaddr()
>
> --
> mail1dotstofanetdotdk
That's exactly what I wanted !
Thanks to you Bjorn, and also Dinh
| |
| surendran.d@gmail.com 2004-12-30, 8:57 am |
| just use
int gethostname(char *name, size_t len);
use the header
#include <unistd.h>, for more information just search
in BEEJ's network programming
all the best
|
|
|
|
|