0

Im using this code:

    final int LOCK_PORT= 54321;
    ServerSocket ss = new ServerSocket(LOCK_PORT);

The thing is that in the same port you cannot listen to 2 different applications (teachers theory).

This code was implemented into an application and the same instance ran more than 1 time. The objective is that the same instance is not suposed to run more than 1 time in the same port. However this isnt working and it does run...

// Edited, more code...

public VentanaPropiedades() {
    initFirst();
    initComponents(); //graphic components of Matisse
}

private void initFirst() {
    loadProperties(); //just internal values of the program, nothing to do

    activateInstance();
}

private void activateInstance() throws Exception {
    try {
    final int LOCK_PORT= 54321;
    ServerSocket ss = new ServerSocket(LOCK_PORT);
    } catch (Exception e) {
        System.out.println(e);
        throw e;
    }
}

private void killProgram() {
    setVisible(false);
    dispose();
    System.exit(0);
}

private void validateInstance() {
    try {
        activateInstance();
    } catch (Exception ex) {
        killProgram();
    }
}

--------------------------Supposed Solution---------------------------
The error catched when the 2nd instance DOES NOT RUN is this one:

 java.net.BindException: Address already in use: JVM_Bind 

However, this error not always happens and you can run more than 1 instance of the same program.

Alpha2k
  • 2,212
  • 7
  • 38
  • 65
  • 1
    What do you mean exactly with "does run"? Does it run, but you get an error? Does it run and you get no error? What happens when someone tries to connect to port 54321? Which instance gets the connection? – Philipp May 06 '14 at 07:44
  • When the instance is ran for first time its suposed that a 2nd instance on the same port shouldnt run, but it does run – Alpha2k May 06 '14 at 07:45
  • Can you show the full code you are using? Maybe the 1st run is already done by the time you start the 2nd run? – Wim Deblauwe May 06 '14 at 07:46
  • 1
    @Alpha2k You didn't answer my question. – Philipp May 06 '14 at 07:47
  • use netstat command to see which process is occupying the LOCK_PORT. – Xing Fei May 06 '14 at 07:49
  • What do you mean exactly with "does run"? (It does run but it gets killed if there is a 2nd instance listening to the same port) Does it run, but you get an error? (the error i suposed to get is java.net.BindException: Address already in use: JVM_Bind but i dont get it always!) Does it run and you get no error? What happens when someone tries to connect to port 54321? Which instance gets the connection? – Alpha2k May 06 '14 at 08:11

2 Answers2

2

It doesn't work. You should get a BindException the second time you try to create the socket. See if you accidentally catch it somewhere or if the port actually is different or something similar.

monocell
  • 1,411
  • 10
  • 14
  • It works more or less, I could catch the exception properly... But it doesnt happen always... – Alpha2k May 06 '14 at 08:28
  • If you can create the socket without an exception it means the other socket is no longer bound. This is not a java thing, your OS gives you this. Try with netcat or something an you will see that it doesn't work either. – monocell May 06 '14 at 08:30
  • @Alpha2k Just because you caught the Exception doesn't mean that it works. If there is an Exception, then there is some error. – Absurd-Mind May 06 '14 at 08:51
  • @Aplha2k, sorry, seems other people report this being unreliable on windows. I had no idea. – monocell May 06 '14 at 08:59
2

The ServerSocket must be declared outside the method, right after main:

public class VentanaPropiedades extends javax.swing.JFrame {
    ServerSocket ss = null;
    // ... more code
}

And the activation method should use the reference:

private void activateInstance() throws Exception {
    try {
        final int LOCK_PORT= 54321;
        ss = new ServerSocket(LOCK_PORT); // note the missing "ServerSocket" before ss
    } catch (Exception e) {
        System.out.println(e);
        throw e;
    }  
}

The problem is that if you create the variable ServerSocket inside a method, the garbage collector will clean it once the method is done. If the variable is declared above, the garbage collector wont collect and clean it because the declared variable will stay instantiated but with NO reference.

Absurd-Mind
  • 7,884
  • 5
  • 35
  • 47
Alpha2k
  • 2,212
  • 7
  • 38
  • 65