The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to delete the files from local host to remote host krishna176 SUN Solaris 3 03-24-2007 12:48 PM
Host ID vs MAC Address TheBlueSky SUN Solaris 4 02-03-2007 06:10 AM
How to get the host ip address in HP-UX in ksh? nir_s Shell Programming and Scripting 12 08-07-2005 05:01 PM
Host IP address for a telnet session shauche UNIX for Advanced & Expert Users 1 05-14-2003 04:33 AM
Server name + IP address removed from host table after rebooting FIRE UNIX for Advanced & Expert Users 3 07-25-2002 12:42 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
host name to IP address

There's a piece in a C program I'm writing (Linux) that simply needs to take a host name and return an IP address (e.g., take 'mail.gnu.org' and return 199.232.76.166). I've gotten a successful status from getaddrinfo, but don't see any of the fields in the result structure that has what I'm looking for, and similar for the structure returned by gethostbyname. I'm wondering if I need to use a function like 'inet_addr' on one of those values to get what I'm looking for. Thank you.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-28-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
That's right. inet_ntoa actually.

gethostbyname(), gethostbyaddr()

Google is your friend.
Reply With Quote
  #3 (permalink)  
Old 03-28-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
I tried this:

struct in_addr host_addr
.
.
.
.

host_addr.s_addr = inet_addr(cat_str);
printf("IP address ==> %s\n", inet_ntoa(host_addr));


All I get is a series of 255.255.255.255 for each of the hosts I try ('cat_str' being something like 'mail.gnu.org'). Maybe I need to pass 'cat_str' into 'gethostbyname' and take the 'host_aliases' out of that and pass it into inet_addr. I guess I'm having a little trouble with the terminology being used by these man pages and making sure I know which form of the address they are referring to.
Reply With Quote
  #4 (permalink)  
Old 03-28-2008
Registered User
 

Join Date: Jun 2006
Posts: 159
There may be a more direct way, but you can get the IP address via ping.

Code:
ping -c 1 hostname.com
ShawnMilo
Reply With Quote
  #5 (permalink)  
Old 03-28-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Did you try the example code at the other end of that link?

inet_addr wants an IP address as a string ("dotted decimal notation", in the parlance), not a host name. Also, you should check its return status, to figure out if something went wrong.
Reply With Quote
  #6 (permalink)  
Old 03-28-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 542
Quote:
Originally Posted by cleopard View Post
There's a piece in a C program I'm writing (Linux) that simply needs to take a host name and return an IP address (e.g., take 'mail.gnu.org' and return 199.232.76.166). I've gotten a successful status from getaddrinfo, but don't see any of the fields in the result structure that has what I'm looking for, and similar for the structure returned by gethostbyname. I'm wondering if I need to use a function like 'inet_addr' on one of those values to get what I'm looking for. Thank you.
Post the C program because without the source it is not possible to figure out why the fields of the structure are not being filled. My guess would be that you have declared a pointer to that structure but not an object of that type. In any case it would be nice to see the code.
Reply With Quote
  #7 (permalink)  
Old 03-28-2008
Registered User
 

Join Date: Dec 2006
Posts: 29
I found a code snippet while via a web search:

struct hostent *hp;
.
.
.

hp = gethostbyname (cat_str);
.
.
.

for (i=0 ......
printf( . . . . . inet_ntoa(*(struct in_addr *)hp->h_addr_list[i]));
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0