0

I have written a simple client server application in java. When i try to run the client on the server I get the below exception. Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at java.net.DualStackPlainSocketImpl.bind0(Native Method) at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source) at java.net.AbstractPlainSocketImpl.bind(Unknown Source) at java.net.PlainSocketImpl.bind(Unknown Source) at java.net.ServerSocket.bind(Unknown Source) at java.net.ServerSocket.(Unknown Source) at java.net.ServerSocket.(Unknown Source) at DateServer.main(DateServer.java:8)

I have added all the jar files checked everything how do I remove this exception to execute my code.

import java.util.Scanner;
import java.net.Socket;

class DateClient {
public static void main(String[] args) throws java.io.IOException {
    String host = "localhost";
    int port = Integer.parseInt("415");
    Socket server = new Socket(host, port);
    Scanner scan = new Scanner(server.getInputStream());
    System.out.println(scan.nextLine());
}

}

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

class DateServer {
public static void main(String[] args) throws java.io.IOException {
    ServerSocket s = new ServerSocket(415);
    while (true) {
        Socket incoming = s.accept();
        System.out.println(incoming);
        PrintWriter toClient = new PrintWriter(incoming.getOutputStream());
        toClient.println(new Date());
        toClient.flush();
        incoming.close();
    }
}

}

Hafiz
  • 37
  • 1
  • 8

3 Answers3

0

According to the API, a BindException

Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned

Basically, that port could be in use by another process, even perhaps by another instance of the same application due to an unclean shutdown. This link may help you troubleshoot.

Note:

The posted results of running the netstat -an command seems to verify that port 8085 is in use by multiple processes, which would result in the BindException that you're encountering

webcoder
  • 656
  • 6
  • 12
0

I've gotten this issue quite a few times. The socket, in your server presumably, still has the port you specified in use or bound by another process (in my case, I'd always have a previous version of the server execution which wasn't killed properly - I'd do Ctrl-C or Ctrl-Z which doesn't kill the process properly).

Try running the command kill %1 or another to completely get rid of the previous run of the process. This usually works for me! :) Good luck!

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
0

You cannot just use any random port without knowing that it's in use or free.

Use netstat -an to check which ports are in use and use one of those which are free. (For windows, use netstat -an in cmd)

Edit: Take a look at below links, one of these must help you How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?

Address already in use: JVM_Bind

Community
  • 1
  • 1
shallOvercome
  • 386
  • 2
  • 10
  • Even after I kill the process the issue remains the same. – Hafiz Nov 11 '13 at 05:09
  • Which process are you killing? Is the serversocket 415 free or being used by some other process? For being on a safer side, I would suggest to first look for a free port and then use it in your server. – shallOvercome Nov 11 '13 at 05:22