tags:

views:

70

answers:

3

i was learning socket programming in unix using c/c++. I am confused with one function call bind(params..). Actually it takes the adreess structure "sockaddr_in" and we can create the structure in the following way

sockaddr_in.*** = somthing..
sockaddr_in..s_addr   htonl(INADDR_ANY)

**Passing INADDR_ANY will alow to bind all local addresses**

My question is , why do we need to use "INADDR_ANY" ? In my knowledge every machine can has only one unique IP Address. In this way there is only one address associated with the machien. Thye bind call should directly bind the socket to the single available address.

Please explain what are the different scenarios and why is it so?

A: 

It's possible for a machine to have more than one IP address, either because it has multiple network cards or through software. INADDR_ANY lets you bind a socket to all of the machine's IP addresses.

JSBangs
A: 

Not true. Most machines are Multi-Homed, that means, they have more than one IP address.

For example, the network address and 127.0.0.1.

Pavel Radzivilovsky
+1  A: 

A machine usually has an IP address for each connected network interface plus 127.0.0.1 for localhost (loopback). For example, a laptop might be connected to a wireless network as 10.0.1.25 and also to a wired network as 10.0.2.4. Servers often have an interface to an internal network as well as a public network.

Justin K