Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Networking - binding to localhost

I have a general question regarding binding and connecting to localhost. I am using a TCP client/server and on the server side I do sth of the form:

bind(localhost, 9999);
listen();

This is done on a unix host with a name e.g. host1

Now, the client is running on a separate Windows box, on the same network. In order to connect I tried to connect via hostname:port, hostIp:port but none of that succeeds.

Is this because binding to localhost is not visible across the network for other processes to connect to and is used for e.g. client/server running on the same machine?

like image 480
Bober02 Avatar asked Dec 14 '22 22:12

Bober02


1 Answers

If you are binding to localhost (i.e. 127.0.0.1), you can only accept connections from the localhost, not over the network.

If you need to accept remote connections over the network, you should either bind to one of the local IP addresses (e.g. 192.168.0.10) or all interfaces (i.e. 0.0.0.0).

This is due to the fact that 127.0.0.1 is always local loopback address, and as such never routed over the network.

like image 59
vhu Avatar answered Dec 28 '22 08:12

vhu