13

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.

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.

Can I inform Windows somehow that I don't even want incoming network connections to be accepted?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • Clarify. You want windows firewall to automatically block all inbound network requests? – Caladain Jul 30 '10 at 21:24
  • 1
    I want the socket listening on the localhost only, *not* to incoming network traffic. In other words, I don't want Windows Firewall showing any pop ups in the first place. – Paul Lammertsma Jul 30 '10 at 21:32

1 Answers1

13

Use the three parameter constructor of the ServerSocket class to specify the IP address as well that the server it 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 Stack Overflow question, for more details.

It is preferable to use InetAddress.getByName(null) to obtain the local address.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • 2
    Great, this works! I had tried that constructor with the last parameter being `InetAddress.getLocalHost()`, but that is evidently the network address of the localhost. Replacing it with `InetAddress.getByName(null)` avoids the firewall pop-up. – Paul Lammertsma Jul 30 '10 at 21:36