We are serving a port on localhost and want to check in another process if the port is available. Due to a bug in our code, it is actually trying to connect to the IP<port>, and for some reason it succeeds -- as strace proves:

connect(3, {sa_family=AF_INET, sin_port=htons(10002), sin_addr=inet_addr("")}, 16) = 0

What does it mean? Why does it work?


1 Answer 1

86 as a target address refers variously to a non-routable host or to “this host” (RFC 5735 section 3). In practice connecting to is, in most scenarios, equivalent to connecting to localhost. Strictly speaking it isn’t valid as a destination address, on the wire (RFC 1122 section, only as a source address, so the operating system has to ensure that a packet with destination address of doesn’t leave the system as-is. In practice, when the Linux kernel sees a packet with a destination address of (i.e. no destination address), it copies the source address to the destination address, and if the packet doesn’t have a source address either, it sets both to the loopback address. In both cases the packet is “sent out” over the loopback interface, so it never leaves the system.

When binding, “this host” expands to “any address on this host” — so applications commonly accept connections by binding to, which means they’ll receive packets addressed to any IPv4 address on the system.

  • 11
    To extend this answer a little - it means "any IP address on this system, including IPs that have been added after the listening process started"
    – Criggie
    Jan 27, 2018 at 9:23
  • 7
    localhost is a single address, mostly, while means all addresses on this host. Jan 27, 2018 at 11:21
  • @rexkogitans localhost is not a single address, but any address in the range - i.e. any address from to
    – Dezza
    Jan 27, 2018 at 15:05
  • 7
    @Dezza No, localhost is (as you say, a.k.a. through is loopback with most equipment, e.g. documented by RFC 5735 page 4. (Interestingly, some Cisco equipment can assign loopback to any address, but defaults to not supporting loopback at all. Not that this is likely to affect other equipment on the network.) However, localhost is a name, typically pointing to just to a single address which is, and commonly implemented by using a "hosts" file. So I disagree with your attempted correction.
    – TOOGAM
    Jan 27, 2018 at 19:11
  • Could you clarify what you mean by "it isn’t valid as a destination address, only as a source address"? When mysql server listens at, is the address destination or source? Is it not the destination of a request sent from a mysql client?
    – Tim
    Mar 22, 2019 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .