linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Remote host name
@ 2004-12-15 15:45 HIToC
  2004-12-15 16:02 ` Ron Michael Khu
  0 siblings, 1 reply; 4+ messages in thread
From: HIToC @ 2004-12-15 15:45 UTC (permalink / raw)
  To: linux-c-programming

Hi all.
I am writing a function that returns the name of the remote host which we are
connected to. About the remote host I only know its IP address...I have tried
to use function "gethostbyname" passing for argument the IP address in
standard dot notation:

[man 3 gethostbyname: The gethostbyname() function returns a structure of 
type hostent for the given host name. Here name is either a host name, or
an IPv4 address in standard dot notation, or an IPv6 address in colon
(and possibly dot) notation.]


struct sockaddr_in destination;

char* get_remote_host_name()
{
  hostent* remote_host_information;
  char* host_IP;

  host_IP =  inet_ntoa(destination.sin_addr);
  remote_host_information = gethostbyname(host_IP);

  return remote_host_information->h_name;
}


This function returns the IP address of the remote host, not its (DNS) name!
Can anybody help me?
-- 

With regards,
				HIToC 
				hitoc_mail@yahoo.it


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Remote host name
  2004-12-15 15:45 HIToC
@ 2004-12-15 16:02 ` Ron Michael Khu
  2004-12-15 17:41   ` Ron Michael Khu
  0 siblings, 1 reply; 4+ messages in thread
From: Ron Michael Khu @ 2004-12-15 16:02 UTC (permalink / raw)
  To: HIToC; +Cc: linux-c-programming

use gethostbyname().....

example:
gethostname( strVariable, len );
printf( "hostname: %s\n", strVariable );

gethostbyname(), on the other hand, is for returning the equivalent ip 
address of  the specified name..
hence the name "gethostbyname"...



HIToC wrote:

>Hi all.
>I am writing a function that returns the name of the remote host which we are
>connected to. About the remote host I only know its IP address...I have tried
>to use function "gethostbyname" passing for argument the IP address in
>standard dot notation:
>
>[man 3 gethostbyname: The gethostbyname() function returns a structure of 
>type hostent for the given host name. Here name is either a host name, or
>an IPv4 address in standard dot notation, or an IPv6 address in colon
>(and possibly dot) notation.]
>
>
>struct sockaddr_in destination;
>
>char* get_remote_host_name()
>{
>  hostent* remote_host_information;
>  char* host_IP;
>
>  host_IP =  inet_ntoa(destination.sin_addr);
>  remote_host_information = gethostbyname(host_IP);
>
>  return remote_host_information->h_name;
>}
>
>
>This function returns the IP address of the remote host, not its (DNS) name!
>Can anybody help me?
>  
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Remote host name
  2004-12-15 16:02 ` Ron Michael Khu
@ 2004-12-15 17:41   ` Ron Michael Khu
  0 siblings, 0 replies; 4+ messages in thread
From: Ron Michael Khu @ 2004-12-15 17:41 UTC (permalink / raw)
  To: Ron Michael Khu; +Cc: HIToC, linux-c-programming

oops..
i meant [gethostname()]  not [gethostbyname()]
=(

sorry

Ron Michael Khu wrote:

> use gethostbyname().....
>
> example:
> gethostname( strVariable, len );
> printf( "hostname: %s\n", strVariable );
>
> gethostbyname(), on the other hand, is for returning the equivalent ip 
> address of  the specified name..
> hence the name "gethostbyname"...
>
>
>
> HIToC wrote:
>
>> Hi all.
>> I am writing a function that returns the name of the remote host 
>> which we are
>> connected to. About the remote host I only know its IP address...I 
>> have tried
>> to use function "gethostbyname" passing for argument the IP address in
>> standard dot notation:
>>
>> [man 3 gethostbyname: The gethostbyname() function returns a 
>> structure of type hostent for the given host name. Here name is 
>> either a host name, or
>> an IPv4 address in standard dot notation, or an IPv6 address in colon
>> (and possibly dot) notation.]
>>
>>
>> struct sockaddr_in destination;
>>
>> char* get_remote_host_name()
>> {
>>  hostent* remote_host_information;
>>  char* host_IP;
>>
>>  host_IP =  inet_ntoa(destination.sin_addr);
>>  remote_host_information = gethostbyname(host_IP);
>>
>>  return remote_host_information->h_name;
>> }
>>
>>
>> This function returns the IP address of the remote host, not its 
>> (DNS) name!
>> Can anybody help me?
>>  
>>
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe 
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Remote host name
       [not found] <31E38B53D182D51195FA00508BE3A33405200607@zwnbc004.cala.nortel.com>
@ 2004-12-16 15:03 ` HIToC
  0 siblings, 0 replies; 4+ messages in thread
From: HIToC @ 2004-12-16 15:03 UTC (permalink / raw)
  To: Sandro Dangui, Ron Michael Khu; +Cc: Linux C programming

On Wednesday 15 December 2004 17:01, Sandro Dangui wrote:
> Try to use gethostbyaddr()
> This is the function that returns the host name.
>
>
>
> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org
> [mailto:linux-c-programming-owner@vger.kernel.org]
> Sent: Wednesday, December 15, 2004 1:46 PM
> To: linux-c-programming@vger.kernel.org
> Subject: Remote host name
>
>
> Hi all.
> I am writing a function that returns the name of the remote host which we
> are connected to. About the remote host I only know its IP address...I have
> tried to use function "gethostbyname" passing for argument the IP address
> in standard dot notation:
>
> [man 3 gethostbyname: The gethostbyname() function returns a structure of
> type hostent for the given host name. Here name is either a host name, or
> an IPv4 address in standard dot notation, or an IPv6 address in colon (and
> possibly dot) notation.]
>
>
> struct sockaddr_in destination;
>
> char* get_remote_host_name()
> {
>   hostent* remote_host_information;
>   char* host_IP;
>
>   host_IP =  inet_ntoa(destination.sin_addr);
>   remote_host_information = gethostbyname(host_IP);
>
>   return remote_host_information->h_name;
> }
>
>
> This function returns the IP address of the remote host, not its (DNS)
> name! Can anybody help me?


Thanks Sandro!
The function now works well using the gethostbyaddr() function:

struct sockaddr_in destination;

char* get_remote_host_name()
{
  hostent* remote_host_information;

  remote_host_information = gethostbyaddr(&destination.sin_addr,
                                                    sizeof(in_addr), AF_INET);
  return remote_host_information->h_name;
}      //...


Thanks Ron, but remember that the function gethostname() is used to access
the host name of the current processor, not the name of the remote host!
Refer to man 2 gethostname...

-- 

With regards,
				HIToC 
				hitoc_mail@yahoo.it


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-12-16 15:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <31E38B53D182D51195FA00508BE3A33405200607@zwnbc004.cala.nortel.com>
2004-12-16 15:03 ` Remote host name HIToC
2004-12-15 15:45 HIToC
2004-12-15 16:02 ` Ron Michael Khu
2004-12-15 17:41   ` Ron Michael Khu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).