Exception itself:

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

  • Platform: Windows 10 x64
  • IDE: IDEA.
  • java version "1.8.0_111" Java (TM) SE Runtime Environment (build 1.8.0_111-b14) Java HotSpot (TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
  • The application is done on javaFX . FXML (If it helps)
  • If I create a new class and describe the creation of a socket there, it will work. But as part of my application on the serverSocket = new ServerSocket(port); the above mentioned crashes.
  • The netstat command did not output the ports I used (4444, 4343, 5555, 5454)

Here are the pieces of code:

Main.java :

 public class Main extends Application{ private Stage primaryStage; private BorderPane rootLayout; private TaskJournal taskJournal; private Database database; private TMServer server; private TableView<Task> taskTable; private ObservableList<Task> taskList; public Main() { taskJournal = TaskJournalController.getInstance(); database = Database.getInstance(); taskList = taskJournal.getTaskList(); server = new ServerController().getInstance(); } @Override public void start(Stage primaryStage) throws Exception{ this.primaryStage = primaryStage; this.primaryStage.setTitle("Task manager"); initRootLayout(); showTaskOverview(); database.set(Database.DatabaseType.SERIALIZE); taskJournal.start(); AlertingSystemController asc = AlertingSystemController.getInstance(); asc.setObservableList(taskList); asc.showSysTray(); asc.runAlertingSystem(); server.start(); //Запуск сервера } 

ServerController.java :

 public class ServerController implements TMServer { private static ServerController instance; private final int port = 5454; private static ServerSocket serverSocket; public ServerController() { try { System.out.println("run server. Port = " + port); serverSocket = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } public ServerController getInstance() { if(instance == null) { instance = new ServerController(); } return instance; } @Override public void start(){ try { new ConnectController(serverSocket.accept()); } catch (IOException e) { e.printStackTrace(); } } @Override public void stop() { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } 

Full e.printStackTrace();

 java.net.BindException: Address already in use: JVM_Bind at java.net.DualStackPlainSocketImpl.bind0(Native Method) at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190) at java.net.ServerSocket.bind(ServerSocket.java:375) at java.net.ServerSocket.<init>(ServerSocket.java:237) at java.net.ServerSocket.<init>(ServerSocket.java:128) at sample.server.controller.ServerController.<init>(ServerController.java:23) at sample.server.controller.ServerController.getInstance(ServerController.java:31) at sample.server.Main.<init>(Main.java:47) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745) 
  • Very similar to a busy port. netstat -a shows these ports? - Anton Shchyrov
  • The fact is that if I create a separate class with my own entry point (psvm), everything will be fine, on the same ports. - Evgeny Tupikov
  • I sin in the direction of extends Application class Main. netstat -a also does not show these ports. - Evgeny Tupikov
  • one
    I suspect that a socket is created somewhere twice - Evgenii Izhboldin

2 answers 2

When executing the line:

 server = new ServerController().getInstance(); 

ServerSocket is created twice.

First, the ServerController() constructor is called, which creates a socket:

 serverSocket = new ServerSocket(port); 

then the getInstance() method is called, during which the instance == null is called and the ServerController() constructor is called a second time:

 instance = new ServerController() 

Your singleton is not at all (and you use it incorrectly). Read about building a singleton .

In the simplest case, the ServerController class will look something like this:

 public final class ServerController { private static ServerController instance; private final int port = 5454; private ServerSocket serverSocket; private ServerController() { try { System.out.println("run server. Port = " + port); serverSocket = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } public static synchronized ServerController getInstance() { if(instance == null) { instance = new ServerController(); } return instance; } ... } 

You can also add double-checked locking here.

  • Then the question is how to fix it? - Evgeny Tupikov
  • Got it. Remove new - Evgeny Tupikov
  • Such a minor mistake, because of my inattention, for which I spent more than one hour ... Thank you! - Evgeny Tupikov
  • @YevgeniyTupikov, you need to rebuild the ServerController class, look at the link I gave. - post_zeew

the Singelton pattern Singelton not correctly implemented, instead of:

 public ServerController getInstance(){} 

need to:

 public static ServerController getInstance(){} 

then instead of:

 server = new ServerController().getInstance(); 

need to:

 server = ServerController.getInstance(); 
  • Yes, thanks to post_zeew, I realized what a mistake! - Evgeny Tupikov
  • And you famously changed the content :) - post_zeew
  • @post_zeew was embarrassed by the first comment that everything is ok in psvm, but it doesn't work in javafx :) - Dmitri88