1

I've wrote these two classes, one for client and the other for server. When I run both of them I get the following error:

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

What is the problem? Also I use TCPview software and there were just two java.exe that use the same port. These two java.exe processes belong to the apps.

Here is the code:

Server Code

/**
*
* @author casinoroyal
*/
public class server {
    public static ServerSocket socket1;
    public static void main(String[] args)  {
        try {
            socket1 = new ServerSocket(1254);
            String request="";
            Socket mylink=socket1.accept();
            System.out.println("server feels=====");
            DataInputStream input= new DataInputStream(mylink.getInputStream());
            DataOutputStream output=new DataOutputStream(mylink.getOutputStream());
            Scanner chat=new Scanner(System.in);

            while(!request.equals("QUIT")){
                request=input.readUTF();
                output.writeUTF(chat.next());
            }

            socket1.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}

Client Code

package javaapplication9;
import java.net.*;
import java.io.*;
import java.util.*;
public class client {
    //main
    public static void main(String[] args)  {
        System.out.println("client want to be connected");   
        try {
            Socket mysock = new Socket(InetAddress.getLocalHost(),1254);               
            System.out.println("client has been connected");  
            DataInputStream input = new DataInputStream(mysock.getInputStream());
            DataOutputStream output = new DataOutputStream(mysock.getOutputStream());
            String reque="";
            Scanner scan1=new Scanner(System.in);
            String sendmsg=scan1.next();

            while(!reque.equals("QUIT")){
                output.writeUTF (sendmsg);
                reque=input.readUTF();
            }

            mysock.close();
        } catch (IOException ex) {
            System.out.println("client rejected"+ex);
        }
    }
}
user812786
  • 3,632
  • 3
  • 36
  • 49
M.shahbazi
  • 17
  • 4
  • probable reason could be that there are two servers up and running. If required use tcpview itself and close both java.exe instances. then retry with first, running server and then client. – Dota2 Jul 26 '17 at 18:13

1 Answers1

-1

What is the problem? Also I use TCPview software and there were just two java.exe that use the same port. These two java.exe processes belong to the apps.

Here is your problem.

You tried to bind 2 sockets at the same port of your computer, and you can't bind 2 sockets at the same port on the same computer.

It's probably because you had an existing process that is listening at the port 1254 ( probably an instance of your server app ), and you tried to run your server app which tried to bind also at the port 1254

user207421
  • 289,834
  • 37
  • 266
  • 440
HatsuPointerKun
  • 607
  • 1
  • 5
  • 14
  • "You can't bind 2 sockets at the same port on the same computer." Actually you can, as long as they are all bound to different IP addresses, and none of those is 0.0.0.0. – user207421 Jul 27 '17 at 01:36
  • i think your right..when i just run the server code(without running the client code)i see two java.exe that runs on the same port...what can i do for it?when i kill one of this files wth TCPview both of java.exe are killed...what can i do? – M.shahbazi Jul 27 '17 at 08:42
  • is there any correction for this code? – M.shahbazi Jul 27 '17 at 08:42
  • @EJP Sorry, i was unclear. You can do twice`new Socket(addressToConnect, aPort);`. The will try to connect at the same port, but their local ports will be differents, so theses two won't use the same port. What i was trying to say is that you can't do twice `new ServerSocket(1254);` will throw a `java.net.BindException`, because you tried to make 2 sockets listen on the same port on the same computer. – HatsuPointerKun Jul 27 '17 at 12:42
  • @M.shahbazi I ran one instance of your server and one instance of your client, and i did not get any `java.net.BindException`. The two clients processes got connected at the server without problems. You just need make sure you don't try to make another server running while there is one running on the computer already. – HatsuPointerKun Jul 27 '17 at 12:56
  • you use netbeans or eclips....i just run the server code once and then run the client code after that.but i always get that eror..what can i do? – M.shahbazi Jul 27 '17 at 16:42
  • @M.shahbazi Make sure no processes uses the port 1254, if you have a server instance running, then close it. After that, re-run your server code – HatsuPointerKun Jul 27 '17 at 18:06