网络-绑定到本地主机 [英] 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();
这是在名称为的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.
这是因为对本地主机的绑定在网络上不可见,其他进程无法连接到该本地主机,例如用于客户端/服务器在同一台计算机上运行?
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?
推荐答案
如果绑定到本地主机(即127.0.0.1),则只能接受来自本地主机的连接,而不能接受网络连接.
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屋!