-2

I'm trying to make an online Chat but when I launch the code I always get java.net.BindException: Address already in use (Bind failed). I've tried changing the Port several times but I always get the same Exception. Sometimes the Code still executes and Everything works fine but the exception is still a problem for me. I know there are questions similar to mine but there are only solutions for windows users and I am a Mac user. Here's the code(It's not completely finished and the error occurs on the Server part @ new ServerSocket();)

public Server(Client c) {

    try {
        ServerSocket serverSocket = new ServerSocket(65531);
        while (true) {
            Socket s = serverSocket.accept();
            new Thread() {
                @Override
                public void run() {
                    super.run();

                    try {
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        String message = bufferedReader.readLine();
                        System.out.println("Antwort vom Client: " + message);
                        c.receivedMessage(message);
                        System.out.println("Adresse: "+s.getInetAddress().toString());
                        System.out.println(s.getInetAddress().getHostAddress());
                        s.close();
                    } catch (Exception e) {

                    }
                }
            }.run();


        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

EDIT:

I've checked the port before after and while the application was running and it is only used when running so the Server Socket is kinda working but I still get an exception

2 Answers2

1

The exception java.net.BindException: Address already in use is trying to tell you that a Java application is trying to connect on a certain port but that port is already used by some other process and JVM Bind to that particular port.

You need to find out which process is already using that port and kill it before trying to connect to the socket. This is how you can do that:

Enter the following in cmd (as administrator):

netstat -ano | findstr :<yourPortNumber>

Then you execute this command after identify the PID:

taskkill /PID <typeyourPIDhere> /F

Run the first command again to make sure the process has been successfully terminated. You will get an empty line if the previous operation succeeded.

Read more: https://javarevisited.blogspot.com/2011/12/address-already-use-jvm-bind-exception.html#ixzz5pCppM2qn

EDIT: Here is how to kill the process on Mac:

netstat -vanp tcp | grep <port number>

If you are using macOS El Capitan or Centos 7 see this answer.

Matthew
  • 1,847
  • 3
  • 16
  • 24
0

Most likely the port is still in use because of a previous run of your program and you did not close the socket correctly. So after an error or incomplete run the socket is still open and therefore the port is used.

Maybe you want to read about try-with-resource or add a finally where you make sure to close your socket if it was created successfully.

Make sure to close all running java processes and/or simply restart your PC to free the port for sure. Then edit your code to handle the port closing correctly and try again.

Björn Böing
  • 1,616
  • 14
  • 21