如何创建仅限localhost的Java套接字? [英] How to create Java socket that is localhost only?
问题描述
我有一个Java服务器,它使用ServerSocket打开一个套接字(使用Thrift)。该服务器在Obj-c中的本地机器上有一个客户端,它与Java服务器通信。一切都发生在localhost上。现在java服务器在网络上也是可见的,我希望java服务器只能在localhost上访问。否则,这是一个潜在的安全漏洞,当防火墙警告用户时,它会吓坏用户。
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)但是这似乎没有任何影响。如何将此内容限制为localhost?
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
这篇关于如何创建仅限localhost的Java套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!