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);
        }
    }
}
1
  • 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. Commented Jul 26, 2017 at 18:13

1 Answer 1

-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

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

7 Comments

"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.
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?
is there any correction for this code?
@EJP Sorry, i was unclear. You can do twicenew 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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.