I have two separate servers running on the same port on a Linux system, one binded to the loopback interface, and the other to the public Ethernet interface. If I connect a client on that system to the public socket, is the system smart enough to keep the traffic internal to itself?
No the data never leaves the box when it's addressing an IP address that's assigned to a local interface.
You can convince yourself of this fact by using
traceroute to confirm.
Here's my wireless NIC on my Fedora 19 system.
$ ip -4 addr show wlp3s0 | grep inet inet 192.168.1.20/24 brd 192.168.1.255 scope global wlp3s0
It's assigned the IP address 192.168.1.20. If we
traceroute this IP:
$ traceroute 192.168.1.20 traceroute to 192.168.1.20 (192.168.1.20), 30 hops max, 60 byte packets 1 greeneggs.bubba.net (192.168.1.20) 0.041 ms 0.013 ms 0.013 ms
Notice we never leave the box. It's a single hop so we're staying local to the system.