简体   繁体   English

单例模式-服务器套接字

[英]Singleton pattern - Server Socket

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. 此代码已实现到应用程序中,同一实例运行了1次以上。 The objective is that the same instance is not suposed to run more than 1 time in the same port. 目的是,同一实例在同一端口上的运行时间不得超过1次。 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. 但是,此错误并不总是发生,并且您可以运行同一程序的多个实例。

It doesn't work. 没用 You should get a BindException the second time you try to create the socket. 第二次尝试创建套接字时,应该获得BindException。 See if you accidentally catch it somewhere or if the port actually is different or something similar. 查看您是否意外将其挂在某个地方,或者端口实际上是否不同或相似。

The ServerSocket must be declared outside the method, right after main: 必须在main之后紧接着在方法外部声明ServerSocket:

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. 问题是,如果在方法内部创建变量ServerSocket ,则方法完成后,垃圾收集器将清除它。 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. 如果在上面声明了该变量,则垃圾收集器将不会收集并清除它,因为声明的变量将保持实例化,但没有引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM