网络 - 绑定到本地主机 [英] Networking - binding to localhost
问题描述
我有一个关于绑定和连接到 localhost
的一般性问题.我正在使用 TCP 客户端/服务器,在服务器端我执行以下形式:
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();
这是在一个名称为 e.g. 的 unix 主机上完成的.host1
This is done on a unix host with a name e.g. host1
现在,客户端在同一网络上的单独 Windows 机器上运行.为了连接,我尝试通过 hostname:port
、hostIp:port
进行连接,但都没有成功.
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.
这是因为绑定到 localhost 在网络上对于其他进程连接是不可见的,并且用于例如客户端/服务器在同一台机器上运行?
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?
推荐答案
如果你绑定到 localhost(即 127.0.0.1),你只能接受来自 localhost 的连接,不能通过网络.
If you are binding to localhost (i.e. 127.0.0.1), you can only accept connections from the localhost, not over the network.
如果您需要通过网络接受远程连接,您应该绑定到本地 IP 地址之一(例如 192.168.0.10)或所有接口(例如 0.0.0.0).
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).
这是因为 127.0.0.1 始终是本地环回地址,因此从未通过网络路由.
This is due to the fact that 127.0.0.1 is always local loopback address, and as such never routed over the network.
这篇关于网络 - 绑定到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!