4

Server Program:

 import java.io.*;
 import java.net.*;
 class Server{
   public static void main(String args[]){
    try{
        ServerSocket ss = new ServerSocket(8080);
        Socket s = ss.accept();
        DataInputStream dis = new DataInputStream(s.getInputStream());
        String str = (String)dis.readUTF();
        System.out.println("Message : "+str);
        ss.close();
    }catch(Exception e){
        System.out.println(e);
    }
  }
}

Client Program:

import java.io.*;
import java.net.*;
class client{
  public static void main(String args[]){
    try{
        Socket s = new Socket("localhost",8080);
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        dos.writeUTF("Hello friend ");
        dos.flush();
        dos.close();
        s.close();
    }catch(Exception e){
        e.printStackTrace();
      }
    }
 }

When I execute this program. I got an error like this "java.net.BindException: Address already in use: JVM_Bind" But before it works fine. Please anyone help me with this issue?

1
  • Perhaps two instances of the same process running at the same time, trying to bind to the same port twice? Commented Aug 6, 2015 at 19:47

1 Answer 1

6

If you're restarting the server side of your program multiple times, there may be sockets in TIME_WAIT hanging around that prevent you from listening on port 8080 again.

You need to set the enable the reuse option (socket option SO_REUSEADDR) as follows:

ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSockAddress(8080));
Sign up to request clarification or add additional context in comments.

Comments

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.