LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 01-05-2005, 04:43 PM   #1
sds
LQ Newbie
 
Registered: Aug 2002
Location: boston
Distribution: redhat
Posts: 5

Rep: Reputation: 0
gethostbyname("localhost")


On linux,
gethostbyname("localhost")
returns a rather useless structure
(the only IP address is 127.0.01).
Thus, when I bind() a socket to it,
the following connect() fails with EINVAL.
(On Cygwin and Solaris gethostbyname() returns the real IP address.)
Questions:

1. what do I do to get the real IP address of the localhost?
(I am not the admin on this linux box, so I cannot edit /etc/hosts)

2. why do I need to bind() the socket at all?
it appears that just removing the bind() call solves the problem on linux!

Thanks.
 
Old 01-05-2005, 04:58 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,925

Rep: Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747
/sbin/ifconfig will output the IP address of each adapter and should be accessible as a regular user.
 
Old 01-05-2005, 05:56 PM   #3
sds
LQ Newbie
 
Registered: Aug 2002
Location: boston
Distribution: redhat
Posts: 5

Original Poster
Rep: Reputation: 0
indeed, ifconfig does provide the necessary information.
now, which system call offers it?
I don't want to call popen() and parse ifconfig output.
 
Old 01-05-2005, 06:24 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,925

Rep: Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747Reputation: 747
The IP address of localhost is always 127.0.0.1.

Using the hostname of the PC instead of localhost should return its "real" IP address. If the PC is configured via DHCP and no DNS then I believe the command will fail.
 
Old 01-06-2005, 05:25 AM   #5
Th3P14Gu3
LQ Newbie
 
Registered: Sep 2004
Location: My computer
Distribution: slackware 9.1, knoppix 3.3.1
Posts: 9

Rep: Reputation: 0
Re: gethostbyname("localhost")

Quote:
Originally posted by sds
On linux,
gethostbyname("localhost")
returns a rather useless structure
(the only IP address is 127.0.01).
Thus, when I bind() a socket to it,
the following connect() fails with EINVAL.
(On Cygwin and Solaris gethostbyname() returns the real IP address.)
Questions:

1. what do I do to get the real IP address of the localhost?
(I am not the admin on this linux box, so I cannot edit /etc/hosts)

2. why do I need to bind() the socket at all?
it appears that just removing the bind() call solves the problem on linux!

Thanks.
for #1 this works, tho some error checking would be nice.......
Code:
#include <netdb.h>
#include <iostream>
#include <arpa/inet.h>
using namespace std;


int main()
{
 struct hostent *p = gethostbyname("localhost");
 
 cout << inet_ntoa( *((struct in_addr *)p->h_addr) )<< endl;
 
 return 0;
}
for #2
you only need to bind if you are listening or have more than one nic.

hope this helps.
 
  


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
Why "bash-2.05b$" in Terminal instead of "username@localhost username" jayelitumbiolo Linux - General 9 10-04-2009 03:16 AM
Apache gives [alert] mod_unique_id: unable to gethostbyname ("") kerrymwarren Linux - Networking 3 08-13-2004 05:01 PM
Apache not starting properly -- "unable to gethostbyname" ColoRambler Linux - Newbie 8 04-24-2004 11:49 PM
"connection refused" on port 993, when not coming from localhost spam4scott Linux - Networking 2 07-14-2003 06:35 AM
/var/log/messages contains "localhost" instead of the real hostname allel Linux - Networking 0 07-15-2002 02:49 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration