2

I am trying to understand a setup and have highly confused my self.

Say my box IP is xx.xx.xx.xx and the 127.0.0.1 is Local Loopback of my Linux box. Now when I do a netstat for a port I see below output:

tcp        0      0 127.0.0.1:11191             0.0.0.0:*                   LISTEN      9999/myexe        off (0.00/0/0)
tcp        0      0 xx.xx.xx.xx:11191           0.0.0.0:*                   LISTEN      26998/anotherexe        off (0.00/0/0)

What does the output basically means - since 127.0.0.1 and xx.xx.xx.xx refers to same box then does it means that two executable have binded and running at same port is same box - if so which binary would service the request if coming at port 11191 in my case?

Programmer
  • 8,303
  • 23
  • 78
  • 162

2 Answers2

2

Each of those is almost certainly a different interface and hence a different internet address. That is, 127.0.0.1 is typically the loopback interface. While presumably xx.xx.xx.xx is a real (ethernet) network interface. It is entirely possible to have two separate programs bound to the same port number on separate addresses. It is more common that a single program simply binds to the port number and the kernel in effect translates that into multiple binds, one for each interface's address.

See bind(2) and ip(7) manual pages for details. Specifically, INADDR_ANY is the pseudo-address that can be used by a server that wishes to bind the port on all interfaces.

See also the answer here under the first paragraph of the Linux subheading:

Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

Community
  • 1
  • 1
Gil Hamilton
  • 11,973
  • 28
  • 51
  • 1
    "*It is entirely possible to have two separate programs bound to the same port number on separate addresses*" - and we can clearly see in the netstat output that is actually the case here. – Remy Lebeau Jan 21 '16 at 02:43
1

On some platforms, netstat can show you the processes that own the sockets. For example, on Windows, the -b switch displays executable names, and the -o switch displays process IDs. On Linux, the -p switch displays process information.

does it means that two executable have binded and running at same port is same box

Yes. Your netstat output includes process names, so we can clearly see that myexe is listening on 127.0.0.1:11191 and anotherexe is listening on xx.xx.xx.xx:11191.

if so which binary would service the request if coming at port 11191 in my case?

It depends on which local IP the connection arrives on. 127.0.0.1 is a loopback adapter, so only clients running on the same machine can connect to it. If a client connects to port 11191 on 127.0.0.1 specifically, myexe will handle the connection. If a client connects to port 11191 on xx.xx.xx.xx, anotherexe will handle the connection.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770