如何创建仅本地主机的Java套接字?
我有一个 Java 服务器,它使用 ServerSocket (使用 Thrift )打开一个套接字。该服务器在 Obj-c 的本地计算机上有一个与 Java 服务器通信的客户端。一切都发生在本地主机上。现在java服务器在网络上也是可见的,我希望java服务器只能在本地主机上访问。否则,这是一个潜在的安全漏洞,当防火墙发出警告时,用户会感到害怕。
我尝试使用 InetSocketAddress('localhost', 9090) 创建服务器套接字,但这似乎没有效果。我怎样才能将这个东西限制为本地主机?
I have a Java server that opens up a socket using ServerSocket (using Thrift with it). This server has a client on the local machine in Obj-c that communicates with the Java server. Everything happens on localhost. Right now the java server is visible on the network as well, I would like the java server to only be accessible on localhost. Otherwise it is a potential security vulnerability and it freaks users out when their firewall warns them.
I tried creating the server socket using an InetSocketAddress('localhost', 9090) but that seems to have no effect. How can I limit this thing to localhost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
摘自另一个问题:
InetAddress.getByName(null)
指向环回地址 (127.0.0.1)和 这里是 Javadoc,它说
Taken from another question:
InetAddress.getByName(null)
points to the loopback address (127.0.0.1)And here's the Javadoc where it says that
让我提出一个仅接受环回设备的替代解决方案。所有其他“localhost”解决方案都会让 Java 选择一个接口。
这是从 Java 7 开始可用的,甚至不会抛出
UnknownHostException
Let me chime in with an alternative solution which only accepts on loopback device. All the other "localhost" solutions will make Java pick an interface.
This is available since Java 7, and does not even throw
UnknownHostException
Try
构造函数的最后一个参数指定将监听套接字绑定到哪个地址。
Try
The last parameter to the constructor specifies which address to bind the listening socket to.