-1

I am writing the following java program for opening a port in Windows. As of my knowledge whenever we are opening a port, Firewall should ask for the permission to allow the access. But here without intimation it is opening. What is the reason behind it.

MyCode

import java.net.*;
import java.io.*;

class Server
{
    public static void main(String[] args) throws Exception
    {
        if(args.length!=1)
        {
            System.out.println("Enter the Port Number");
            return;
        }
        int portNumber = Integer.parseInt(args[0]);
        ServerSocket ss = new ServerSocket(portNumber);
        while(true)
        {
            Socket socket = ss.accept();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String message = (String)ois.readObject();
            System.out.println("Client: "+message);
            if(message.equals("exit")) break;
            ois.close();
        }
        ss.close();
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
KCS
  • 442
  • 5
  • 20
  • Are you sure the firewall cares whether you open the port? Shouldn't it tntercept all connection requests to the port instead? – Thomas Stets Nov 21 '16 at 12:04
  • Does your code throw an exception? If not and you can communicate with the other end, there is no problem then. – Kevin G. Nov 21 '16 at 12:29
  • @ThomasStets when we are installing Servers like ApacheTomcat, Firewall will popup and ask the permission to allow access. That is the reason I thought when we write a program to open/create a port it should pop up. – KCS Nov 21 '16 at 12:48
  • Once you've given permission it won't ask again. Nothing to do with [tag:java] whatsoever, or computer programming either. Off topic. – user207421 Nov 21 '16 at 17:20

1 Answers1

1

The Windows Firewall will prompt the user to "allow an application" whenever it sees a program open a listening socket port that doesn't have a rule associated with it. (i.e. new programs). But when the user clicks "allow" on the confirmation dialog, it's for the entire program, not just one port. And it typically does not prompt the user again once confirmation is given.

In the case of Java, the running program is Java.exe. So once the user clicks "allow" to the first program running Java, all subsequent Java programs get enabled without prompting. I would also suspect that the Java installer just sets a Firewall rule for itself as well. I seem to have several:

In other words, you already got Java registered, so you aren't getting prompted again.

selbie
  • 100,020
  • 15
  • 103
  • 173