0

I am Running into a java.net.BindException: Address already in use (Bind failed) on a server-client socket app

I am trying to learn about Java sockets using a Youtube tutorial as a reference. My code seems to match everything in the video (except variables names) but, when trying to run the server and then the client sockets, I get:

Exception in thread "main" java.net.BindException: Address already in use (Bind failed)

I have tried even printing out the local port just to make sure I connect to the right available port but, nothing works. Is there any documentation I can look into to solve this problem? or any guidance?

Server.java

public class serverSocket {
 public static void main(String args[]) throws IOException{

  String message, serverResponse;

  ServerSocket serverSocket =  new ServerSocket(56789);
  System.out.print(serverSocket.getLocalPort());
  Socket acceptClientRequestSocket = serverSocket.accept();
  Scanner serverScanner = new Scanner(acceptClientRequestSocket.getInputStream());
  message = serverScanner.next();
   System.out.println(message);
  serverResponse = message.toUpperCase();

  PrintStream newMessage = new PrintStream(acceptClientRequestSocket.getOutputStream());
  newMessage.println(serverResponse);    

 }
}

Client.java


public class clientSocket {

public static void main(String args[]) throws UnknownHostException, IOException {

    String message,outputMessage;
    Scanner clientInput =  new Scanner(System.in);
    Socket clientSocket = new Socket("localhost",56789);
    Scanner incomingStream = new Scanner(clientSocket.getInputStream());
    System.out.println("Enter a message");
    message = clientInput.next();
    PrintStream printClientStream=  new PrintStream(clientSocket.getOutputStream());
    printClientStream.println(message);
    outputMessage = incomingStream.next();
    System.out.println(outputMessage);

}
}

Is there any documentation I can look into to solve this problem? or any guidance?

1
  • 1
    Please post the entire exception and stack trace in your question, and always when you post Java questions on this site. This code is OK but in general please don't waste your time with YouTube tutorials or other arbitrary Internet junk. Use something authorititave: in this case, the Custom Networking section of the Oracle Java Tutorial. Commented May 6, 2019 at 2:21

1 Answer 1

3

You have probably your previously exectued program still running. Check the running java processes. Kill the the previous one and try again.

If this wouldn't help try restarting your machine. If the problem persists after that then some service is already running on this port and is starting with the OS. In that case you can either change the port number in your app or disable that service.

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

3 Comments

@Krysztof Cichocki, I tried that and still didn't get to run the client socket. : /
@Monique Please wait four minutes after stopping both client and server and try again.
Thank you @Krzysztof. I have done it all an nothing seems to work even so you guys agreed that there isn't anything wrong with the code. :( i will keep on researching but thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.