如何创建仅本地主机的Java套接字?

发布于 2024-08-20 12:19:21 字数 270 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撧情箌佬 2024-08-27 12:19:21

摘自另一个问题:

new ServerSocket(9090, 0, InetAddress.getByName(null));

InetAddress.getByName(null) 指向环回地址 (127.0.0.1)

这里是 Javadoc,它说

Taken from another question:

new ServerSocket(9090, 0, InetAddress.getByName(null));

InetAddress.getByName(null) points to the loopback address (127.0.0.1)

And here's the Javadoc where it says that

戒ㄋ 2024-08-27 12:19:21

让我提出一个仅接受环回设备的替代解决方案。所有其他“localhost”解决方案都会让 Java 选择一个接口。

new ServerSocket(9090, 0, InetAddress.getLoopbackAddress());

这是从 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.

new ServerSocket(9090, 0, InetAddress.getLoopbackAddress());

This is available since Java 7, and does not even throw UnknownHostException

差↓一点笑了 2024-08-27 12:19:21

Try

new ServerSocket(9090, 0, InetAddress.getByName("localhost"))

构造函数的最后一个参数指定将监听套接字绑定到哪个地址。

Try

new ServerSocket(9090, 0, InetAddress.getByName("localhost"))

The last parameter to the constructor specifies which address to bind the listening socket to.

睫毛上残留的泪 2024-08-27 12:19:21
new ServerSocket(9090, 0, InetAddress.getByName(null));
new ServerSocket(9090, 0, InetAddress.getByName(null));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文