By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,706 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,706 IT Pros & Developers. It's quick & easy.

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

P: 16
Hi, i am using NetBeans IDE 6.0.1 and i created a server project and a client project, first i run the server that listens to the port "8221" and waits for clients accessing that port, and when i run the client that accesses the server at port "8221" the IDE throws this exception
"Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)"

By the way, i checked for any process using the same port in the command window by typing "netstat -a" and i didn't find any.

The server code:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.             ServerSocket server = new ServerSocket(8221);
  3.             System.out.println("Waiting for clients\n");
  4.             Socket connection = server.accept();
  5.             ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
  6.             ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
  7.             StringBuffer buff = new StringBuffer();
  8.             do {
  9.                 int c = 0;
  10.                 while (((c = input.read()) != '\n') || ((c = input.read()) != -1)) {
  11.                     buff.append(c);                    
  12.                 }
  13.                 msg = buff.toString();
  14.             } while (msg != "Exit");
  15.             System.out.println(msg);
  16.         } catch (IOException ex) {
  17.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  18.         }
  19.  
The client code:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.  
  3.             Socket client = new Socket(InetAddress.getLocalHost(),8221);
  4.             //Socket client = new Socket("192.168.1.18",8221);
  5.             ObjectInputStream input = new ObjectInputStream(client.getInputStream());
  6.             ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
  7.             String msg = "Hello Exit";
  8.             output.writeBytes(msg);
  9. //            StringBuffer buff = new StringBuffer();
  10. //            do {
  11. //                int c = 0;
  12. //                while (((c = input.read()) != '\n') || ((c = input.read()) != -1)) {
  13. //                    buff.append(c);
  14. //                }
  15. //                msg = buff.toString();
  16. //            } while (msg != "Exit");
  17.         } catch (IOException ex) {
  18. //            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  19.             System.out.println(ex);
  20.         }
  21.  
Thanks.
Nov 6 '08 #1
Share this Question
Share on Google+
1 Reply


100+
P: 127
That's because the Java program/process is still running. Did you abruptly shutdown your Netbeans IDE?

Try closing Netbeans and run the program again. It should work fine!
Nov 6 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.