简体   繁体   中英

ServerSocket port issue (java.net.BindException: Address already in use: JVM_Bind)

I'm building a simple client / server program on Java using Sockets. I've followed several tutorials on creating a simple client / server program which has got me this far, but when I try and run my server class, it either freezes when I type in certain ports and does nothing, or with some other ports it gives me the exception "java.net.BindException: Address already in use: JVM_Bind".

Here's some of the key parts to my server class;

public void start() throws IOException {
    isRunning = true;
    serverSocket = new ServerSocket(getPort());
    while (isRunning) {
        try {
            clientConnection();
            setupStreams();
        } catch (IOException ex) {
            addOutput("Connection unsuccessful.");
            System.out.println(ex);
            System.exit(1);
        }
    }
}

private void clientConnection() throws IOException {
    addOutput("Listening for client...");
    socket = serverSocket.accept();
    addOutput("Connection successful."); // print out username which connected
}

private void setupStreams() throws IOException {
    serverIn = new ObjectInputStream(socket.getInputStream());
    serverOut = new ObjectOutputStream(socket.getOutputStream());
}

private void btnConnectActionPerformed(ActionEvent evt) {                                           
    if (evt.getSource() == this.btnConnect) {
        try {
            start();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}  

Like I said, I've followed tutorials and there's not much difference if any between what I've done compared to them, but I'm getting issues and my server is just doing nothing and freezes, or gives that exception.

Any ideas?

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

happens when you try to run your program using a port number which is already in use, either by other programs on your system or another of your program instances that "freezed".

as for freezing,

socket = serverSocket.accept();

will wait till there is connection made to this socket by a client. Without another program running as client this one will "freeze" waiting for one.

I changed my code around a bit and it seems to have fixed the problem. The main fix was setting the server up in the constructor, that seemed to have fixed it. Although, I still don't understand why the way I was doing it before wasn't working. But, it works now.

The above error comes only when the server/tomcat is using the same port.

On Mac Please use the below commands :

I514W:~ skumar$ lsof -i :8082
COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    45270 skumar47  411u  IPv6 0x34323c1bdd54744d      0t0  TCP *:us-cli 
(LISTEN)

I514W:~ skumar$ kill 45270

First one(lsof -i :8082) will list the process running on that port. And second one (kill 45270) will kill the process and port will be free. Once you restart the tomcat or any other process which was using that port it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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