问题描述
我有一个 Java 服务器,它使用 ServerSocket(使用 Thrift)打开一个套接字.该服务器在 Obj-c 中的本地机器上有一个客户端,它与 Java 服务器进行通信.一切都发生在本地主机上.现在java服务器在网络上也是可见的,我希望java服务器只能在本地主机上访问.否则,这是一个潜在的安全漏洞,当他们的防火墙警告用户时,它会吓坏用户.
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.
我尝试使用 InetSocketAddress('localhost', 9090) 创建服务器套接字,但这似乎没有效果.我怎么能把这个东西限制到本地主机?
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?
推荐答案
new ServerSocket(9090, 0, InetAddress.getByName(null));
InetAddress.getByName(null)
指向环回地址(127.0.0.1)
InetAddress.getByName(null)
points to the loopback address (127.0.0.1)
和 这里是 Javadoc,它说
And here's the Javadoc where it says that
这篇关于如何创建仅限本地主机的 Java 套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!