Re: How can a server know its own IP address
From: Pascal Bourguignon (spam_at_mouse-potato.com)
Date: 09/28/05
- Next message: eyal.rif_at_gmail.com: "Socket Resource temporarily unavailable"
- Previous message: Ravi Uday: "Re: How can a server know its own IP address"
- In reply to: au.faisal: "How can a server know its own IP address"
- Next in thread: Jonathan Bartlett: "Re: How can a server know its own IP address"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 28 Sep 2005 12:21:46 +0200
"au.faisal" <au.faisal@gmail.com> writes:
> I am a newbie in socket programming. Can anyone tell me how can a
> server know on which IP address it is running.
>
> To make it suppose a webserver is running on computer which has the IP
> address 131.100.0.1. How can the webserver get this IP server without
> hardcoding.
You've got a misconception here. Computers DO NOT HAVE IP addresses.
Interfaces HAVE.
And since there's always at least the lo0 interface, all computer that
has a physical network interface has at least two interfaces with each
one IP address.
Moreover, most in implementations several IP addresses can be assigned
to each physical interface.
So you should ask: how can I get the list of interfaces my computer
has, and how can I get the list of IP addresses one interface has?
> is there any function for that?
gethostbyname won't really help here, since it only returns what's
stored into the DNS database.
The user command to use is ifconfig(1)
There's no standard API to get the list of interfaces,
But there's one to get the list of IP addresses: getaddrinfo(3).
It returns a list of IP addresses, you'll have to filter out and
select the one you're interested in.
Note that most often, the servers are configured to answer to requests
from ANY address. And the TCP/IP protocol stack is designed to select
for outgoing packets the interface (therefore source address) that is
the most propicious to get a good connection to the destination address.
Which leaves me to wonder why you need to know "which IP address it is running"?
This is a rather meaningless question.
If you want to know on what IP address the given web server listens
to, the best way is to read it's configuration files.
For example, here:
$ grep -i listen /etc/httpd/httpd.conf
# Listen: Allows you to bind Apache to specific IP addresses and/or
#Listen 3000
#Listen 12.34.56.78:80
# is used to tell the server which IP address to listen to. It can either
# See also the <VirtualHost> and Listen directives.
# Port: The port to which the standalone server listens. For
## When we also provide SSL we have to listen to the
Listen 80
Listen 443
Since there's no specific IP address configured, my httpd server
listens on ALL the IP addresses of this host. What more do you need to know?
-- __Pascal Bourguignon__ http://www.informatimago.com/ Small brave carnivores Kill pine cones and mosquitoes Fear vacuum cleaner
- Next message: eyal.rif_at_gmail.com: "Socket Resource temporarily unavailable"
- Previous message: Ravi Uday: "Re: How can a server know its own IP address"
- In reply to: au.faisal: "How can a server know its own IP address"
- Next in thread: Jonathan Bartlett: "Re: How can a server know its own IP address"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|