LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-01-2009, 06:00 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Rep: Reputation: 15
How to get host name from host ip using C


I know host ip of system in domain.But I don't know the hostname of that system.

Please give small example to get host name from host IP using C.

ThanQ,
Nagendra Rednam
 
Old 12-01-2009, 07:31 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Look up the gethosthostbyaddr() function - you can use the Linux manpages or search online.

John G
 
Old 12-01-2009, 10:18 PM   #3
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Original Poster
Rep: Reputation: 15
I got hostname following way

int main(){
hostent * sHostent = 0;
char hostname[1024];

unsigned long addr = inet_addr("<IP>");
if (addr != INADDR_NONE)
sHostent = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);

if (sHostent) //Host address is a name
{
sprintf(hostname, "%s", sHostent->h_name);
printf("===%s",hostname);
}
}
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind9 ,host does reslove a dns name but ping says unknown host Byenary Linux - Networking 10 01-12-2011 01:33 AM
try install host ; bind9-host uninstalled , how to undo sudo apt-get install host? shojaru Linux - Newbie 0 06-11-2009 12:45 AM
Establishing a VPN connection (host to host) using IPSec services adithya24 Linux - Networking 9 06-10-2009 08:44 AM
Required to achieve host to host communication via USB file transfer cable may1284 Linux - General 1 03-14-2007 04:02 PM
host-to-host USB cable: how to make it work under debian?? stefan_578 Linux - Hardware 18 12-20-2006 02:09 PM

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

All times are GMT -5. The time now is 11:55 PM.

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