title: how to accept only 1 connection in socket
body: <p>Hi i have this code of the server and i added the if to check if ocupado(busy) is = 0, accept socket, if its open busy = 1, so reject till the first user finish the connection, but dont work...</p>
  
  <pre><code>import java.io.ObjectOutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.Date;
  
  /**
   *
   * @author www
   */
  public class ServidorTCPBasico {
  static int ocupado = 0; // = busy var to check
    public static void main(String[] args) {
  
      try {
  
        // Instancia o ServerSocket ouvindo a porta 9000
  
        ServerSocket servidor = new ServerSocket(9000);
  
        System.out.println("Servidor ouvindo a porta 9000");
  
        while(true) {
  
          // o método accept() bloqueia a execução até que
  
          // o servidor receba um pedido de conexão
          Socket cliente = servidor.accept();
          if(ocupado == 0) { 
  
          int ocupado = 1;
          System.out.println("Cliente conectado: " + cliente.getInetAddress().getHostAddress());
  System.out.println(ocupado);
          ObjectOutputStream saida = new ObjectOutputStream(cliente.getOutputStream());
  
          saida.flush();
  
  //        saida.writeObject(new Date());
          saida.writeUTF("TEXTO TESTE PARA TRANSFERIR...");
          saida.close();
  
        //  cliente.close();
  
          }  else { 
              System.out.println("Ocupado");
            }
  
        }   
      }
  
  
      catch(Exception e) {
  
         System.out.println("Erro: " + e.getMessage());
  
      } 
  
      finally {
      }  
  
  
    }     
  
  }
  </code></pre>
  
  <p>so my question is how to make the socket to accept onnly one connection per time, and THE SERVER close the connection when the user press OK in a confirm box..</p>
  
  <p>the part of confirm box etc.. i will add, my problem is the "logic" or command to allow only one connection, when the client try, it refuse the connection, i will treat the refused connection in the client later</p>