'Java sockets cannot reuse port

i have a java server-client program with sockets. I need to reuse the same port for a connection. I have try to use

         serverSocket.setReuseAddress(true);

but i always get ConnectException: Connection refused: connect

This is just a part of the program. I have to close the socket-port connection i reopen it in the same port if needed.

Any ideas will be helpful

Thanks in Advance!

server :

 class ser  extends Thread {
 int port;
  public ser(int port) {
    this.port = port;
 }   
@Override
public void run() {
    try  {
        ServerSocket serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(port));

        
        Socket clientSocket = serverSocket.accept();
        System.out.println("Server started");
        System.out.println("Accepted connection");
               
       Scanner clientIn = new Scanner(clientSocket.getInputStream());
       
        PrintWriter clientOut = new PrintWriter(clientSocket.getOutputStream(), true);
           do  {
           
            String message = clientIn.nextLine();
            System.out.println("Received: " + message);
            clientOut.println(message);
                    
           }  while (clientIn.hasNextLine());
   
        clientOut.close();
        clientIn.close();
        clientSocket.close();
        serverSocket.close();
        System.out.println("closed");
          


    }

    catch (IOException e)  {
        System.err.println(e);
        System.exit(1);
    }
    catch (Exception e){
    
    }

        }   
      }

client:

 class cl extends Thread{
 String msg="";
 int port;

 public cl(int port) {
 this.port=port;
 }

  @Override
  public void run() {
     
    try  {
        Socket echoSocket = new Socket("localhost",port);
        System.out.println("Client started.");
        
        PrintWriter serverOut = new PrintWriter(echoSocket.getOutputStream(), true);
        Scanner serverIn = new Scanner(echoSocket.getInputStream());
        
        Scanner keyboard = new Scanner(System.in);
        //while ( /*!msg.equals("")*/true)  {
            
            System.out.println("msg "+Broker_GUI.publishmessage[2]);
            serverOut.println(Broker_GUI.publishmessage[2]);
            String response = serverIn.nextLine();
            System.out.println("Response: " + response);
           
      // }
        
        serverIn.close();
        serverOut.close();
        echoSocket.close();
        keyboard.close();
        System.out.println("closed");
    }
    catch (UnknownHostException e)  {
        System.err.println("Unknown host: " + e);
        System.exit(1);
    }
    catch (IOException e)  {
        System.err.println("Could not communicate with server: " + e);
        System.exit(1);
    }       
    }//end run
   }//end class


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source