0

I am trying to build a java microblogging app I have finished the code but cannot connect to my own computer due to the following error (I google it and someone said I need to change the port number. I changed the port number and nothing happened)

Exception in thread "Thread-4" java.net.BindException: Address already in use: JVM_Bind

Below is the code for the server:

public class server extends Thread {
    public Socket client = null;
    public ServerSocket server = null;
    private int port = 4444; 

public void run(){  
    while (true){ //loop waits for incomming connection
        try { //start the server and listen to a port
                server = new ServerSocket(port);
            }catch (IOException e){
                System.err.println(e);
                System.exit(-1);
            }

            try { //establish a connection when receiving request
                client = server.accept();
            }catch (IOException e){
                System.err.println(e);
                System.exit(1);
            }

            Thread t = new Thread(new Connection(client));
            t.start();
        }
    }
}

And this is the code to start the server and listen to port 4444

Server server = new Server();
server.start(); //to listen to a port

Thank you

5
  • 4444. the default port is always 4444 thank you Commented Nov 3, 2012 at 1:12
  • No, I mean,that error means port 4444 is already in use. What other ports have you tried?you can use "netstat -pan" to list the ports used in your system Commented Nov 3, 2012 at 1:14
  • But 4444 is not used for anything on my computer. Or that means I cannot connect to my own computer but have to use a different com? Commented Nov 3, 2012 at 1:16
  • You can connect to your own computer. try the netstat command from a cmd console, are you sure you are not trying to launch multiple copies of your app? Commented Nov 3, 2012 at 1:19
  • Create server socket out of the loop and accept connections within the loop. Also close any sockets when done or exiting the program. Commented Nov 3, 2012 at 1:20

1 Answer 1

1

You must create the ServerSocket before entering the loop. At present you are trying to create it every iteration, which doesn't make sense, and you aren't closing it, so the second creation fails.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.