65

I don't understand what it means to bind a socket to any address other than 127.0.0.1 (or ::1, etc.).
Am I not -- by definition -- binding the socket to a port on my own machine.. which is localhost?
What sense does it make to bind or listen to another machine or IP address's port?
Conceptually, it just doesn't make sense to me!

(This has proven surprisingly hard to Google... possibly because I'm not Googling the right terms.)

1
  • What it means to bind a socket to any IP address other than INADDR_ANY, where 'other' includes localhost, is that it will only accept connections made to that IP address.
    – user207421
    Apr 15, 2019 at 19:58

1 Answer 1

77

Binding of a socket is done to address and port in order to receive data on this socket (most cases) or to use this address/port as the source of the data when sending data (for example used with data connections in FTP server).

Usually there are several interfaces on a specific machine, i.e. the pseudo-interface loopback where the machine can reach itself, ethernet, WLAN, VPN... . Each of these interfaces can have multiple IP addresses assigned. For example, loopback usually has 127.0.0.1 and with IPv6 also ::1, but you can assign others too. Ethernet or WLAN have the IP addresses on the local network, i.e. 172.16.0.34 or whatever.

If you bind a socket for receiving data to a specific address you can only receive data sent to this specific IP address. For example, if you bind to 127.0.0.1 you will be able to receive data from your own system but not from some other system on the local network, because they cannot send data to your 127.0.0.1: for one any data to 127.0.0.1 will be sent to their own 127.0.0.1 and second your 127.0.0.1 is an address on your internal loopback interface which is not reachable from outside.

You can also bind a socket to a catch-all address like 0.0.0.0 (Ipv4) and :: (Ipv6). In this case it is not bound to a specific IP address but will be able to receive data send to any IP address of the machine.

17
  • 1
    +1 Thanks for the answer. This raises several questions for me actually: (1) So is it correct to say that the "address" that I bind to is actually specifying the interface that I'm binding to? (2) Notwithstanding the first part, what if I have the same address on two interfaces? Will it bind to both simultaneously? (3) Is it actually true that binding to 127.0.0.1 prevents other systems from sending me packets from a security standpoint? Can't they manually send a packet that specifies that as a bogus IP address for the target? (4) Is a socket bound to a bogus address 100% unreachable?
    – user541686
    Sep 4, 2016 at 6:51
  • 1
    @Mehrdad: no, you are not binding to the interface but to the address on the interface. Bindung to 127.0.0.1 will not receive data for 127.1.1.1 even if this might be the same interface. (2) you cannot have the same IP on different interface and if you do chaos will occur (i.e. undefined). (3) yes, binding to 127.0.0.1 restricts access to all systems which can reach your 127.0.0.1 - which should be only your system. Most systems will reject or drop packets which arrive on an interface where the target address is not configured. Sep 4, 2016 at 6:55
  • 1
    (4) if nobody is able to send you data to the bogus address then you will not be able to receive data there. This makes the socket effectively unusable in most cases. Sep 4, 2016 at 6:56
  • 1
    @Mehrdad: if you have such a setup and a TCP SYN for the IP address comes in then your system might send out the ACK on a different interface because this one claims to be in the same network. That's why I mean it will be chaos, i.e. it might work or might not work or sometimes work etc. The behavior might also be depend on the specific OS. Sep 4, 2016 at 9:40
  • 3
    @Mehrdad: no idea what is awful with that. Just imagine you have a city where you have the same street name (i.e. IP address) multiple times in different parts of the city (i.e. interface). Unless you have some other way to distinguish the streets in the address (i.e. ZIP code) chaos will happen when trying to deliver the mail. Sep 4, 2016 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.