nairware
nairware

Reputation: 3190

Exception in thread "main" java.net.BindException: Address already in use - Error in Netbeans only

On my machine, the following code compiles within Eclipse but throws an exception within Netbeans. The error message says "Exception in thread "main" java.net.BindException: Address already in use".

What is the proper configuration within Netbeans to make this code compile? It seems like the problem has to do with the fact that I have two main functions. If I start running either one of the apps, the second will fail to start, throwing the exception posted above.

Server.java

import java.io.*;
import java.net.*;

public class Server {

    public static void main(String[] args) throws Exception {

        Server myServ = new Server();
        myServ.run();

    }

    public void run() throws Exception {

        ServerSocket mySS = new ServerSocket(9999);
        Socket SS_accept = mySS.accept();

        InputStreamReader mySR = new InputStreamReader(SS_accept.getInputStream());
        BufferedReader myBR = new BufferedReader(mySR);

        String temp = myBR.readLine();
        System.out.println(temp);

    }

}

Client.java

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {

        Client myCli = new Client();
        myCli.run();

    }

    public void run() throws Exception {

        Socket mySkt = new Socket("localhost", 9999);
        PrintStream myPS = new PrintStream(mySkt.getOutputStream());

        myPS.println("Hello server");

    }

}

Upvotes: 7

Views: 47393

Answers (6)

Aayaan Gautam
Aayaan Gautam

Reputation: 1

I change the port address and it work for me in the Neat Beans IDE . This problem will come if we used the same port address for other one times . so to fix this error you have to change the port address and I am sure it will work

Upvotes: 0

Xan
Xan

Reputation: 31

I did try the method catch and solved the problem.

Upvotes: -1

Hansa Tharuka
Hansa Tharuka

Reputation: 137

Server.java

public class SocServer {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(5001);
            Socket client = server.accept();
            DataOutputStream os = new DataOutputStream(client.getOutputStream());
            os.writeBytes("Hello Sockets\n");
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Client.java

public class SocClient {

    public static void main(String[] args) {
        try {
            Socket socClient = new Socket("localhost", 5001);
            InputStream is = socClient.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String receivedData = br.readLine();
            System.out.println("Received Data: " + receivedData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

refer above code and it works for me..

Upvotes: -1

nairware
nairware

Reputation: 3190

The way to achieve what I want is to right-click on the particular class (ex. Server.java) that I want to run and select "Run this file". This enables me to run only the Server app. Then, do the same process for the other file, Client.java.

However, Netbeans is somewhat confusing/deceiving in this particular circumstance. What Netbeans does is it runs the Server process, but labels that process as the name of the project (ex. MyTestNetworkingProject) and puts a run number on it, thus giving us MyTestNetworkingProject run #1 (it actually leaves out the #1 on the first process). Then, if I go to the Client.java file and select "Run this file", it generates a second process, MyTestNetworkingProject run #2. It then generates a second results window down at the bottom of the screen, as it generates these in new tabs as new processes get created.

Because of the nature of my specific code, what I wanted to see in my results window to confirm that my application was working was I wanted to observe the Server.java results window (which in this case is MyTestNetworkingProject run #1). Given my exact sequence of steps outlined above of running the different files, run #2 is the last run process and thus the tab on top, covering the run #1 tab. I can click on run #1 and see the results I was hoping to see in the console ("Hello server"), but I just have to know/remember that MyTestNetworkingProject run #1 represents the Server app and not the Client app.

Uncool, IMO.

Upvotes: 4

user207421
user207421

Reputation: 310979

The problem is due to the fact that you left one instance of your server running and then started another one.

Upvotes: 5

William Feirie
William Feirie

Reputation: 644

If you write this in Windows OS,you can use "netstat -nao" to see which process use the 9999 port.If it is some unimportant process,you can kill this process.Otherwise you can change the port of the pragram.

Upvotes: 0

Related Questions