我编写了一个简单的Java应用程序,它使用套接字与自身的多个实例进行交互。第一个实例自动承担服务器的角色,侦听特定端口,并且所有后续实例都连接到它。
I have written a simple Java application that interacts with multiple instances of itself using sockets. The first instance automatically takes on the role of the server, listening on a specific port, and all subsequent instances connect to it.
我面临的问题是Windows防火墙弹出询问我是否要取消阻止该程序接受传入的网络连接。问题是:如果你让应用程序被阻止并不重要,因为应用程序的实例总是在同一台机器上,所以它总能工作。
The problem I'm faced with is that Windows Firewall pops up asking me if I want to unblock the program from "accepting incoming network connections". The thing is: it doesn't matter if you leave the application blocked, because the instances of the application are always on the same machine, so it will always work.
我的问题是:我能否以某种方式告知Windows我甚至不想接受传入的网络连接?
My question is: can I inform Windows somehow that I don't even want incoming network connections to be accepted?
推荐答案使用 ServerSocket类的三个参数构造函数,用于指定服务器应该监听的IP地址。这样,您可以将服务器限制为仅在127.0.0.1上侦听,而不像默认值0.0.0.0。有关详细信息,请参阅此相关SO问题。
Use the three parameter constructor of the ServerSocket class to specify the IP address as well that the server should listen on. That way you can restrict the server to listen only on 127.0.0.1, unlike the default of 0.0.0.0. See this related SO question, for more details.
编辑:最好使用InetAddress.getByName(null)来获取本地地址。
EDIT: It is preferable to use InetAddress.getByName(null) to obtain the local address.
更多推荐
避免使用localhost上的套接字弹出Windows防火墙
发布评论