title: Java : Client-Server Simple Application with IP Blacklist
body: <p>I'm writing a client-server application in simple java (<code>java.net*</code> and <code>java.io*</code>) with multi-threading to enable connections of several clients to the server. I'm also trying to implement a simple IP blacklist in a text file, which gets accessed when someone connects to the server socket, and closes the connection if the IP is there, or lets the client thread start if its not there.</p>
  
  <p>The blacklist is working for the IPs that need to be blacklisted and for those it won't even start a thread.</p>
  
  <p>The problem here is the non-blacklisted IPs, even though they start a new thread and establish a connection, don't reach the input part of the application:</p>
  
  <p>Server:</p>
  
  <pre><code>        ServerSocket server = new ServerSocket(6500);
      System.out.println ("server started on port 6500");     
      while (true){//waiting for clients
          Socket socket = null;
          BufferedReader reader = new BufferedReader(new FileReader("C:\\UNIV\\Redes\\workspace\\Copy of Ex_4.3_Teste\\lists\\blacklist.txt"));
          String line = null;
          socket = server.accept();
  
          // BlackList Verification
          while ((line = reader.readLine()) != null) {
              if (line.equals(socket.getInetAddress().toString())) {
                  System.out.println("IP Blacklisted: " + socket.getInetAddress().toString());
                  System.out.println("Terminating connection with " + socket.getInetAddress().toString());
                  PrintStream checkBlack = new PrintStream(socket.getOutputStream(),true);
                  checkBlack.println("***BLACKLISTED***");
                  reader.close();
                  socket.close();
                  break; 
              }
          }//end of blacklist verification
  
          userList.add(socket.getInetAddress().toString());
          System.out.println("new connection...");
          System.out.println("Size of UserList: " + userList.size());
          Thread t = new Thread(new EchoClientThread(socket)); 
  
          t.start();
  </code></pre>
  
  <p>And the Client:</p>
  
  <pre><code>public static void main(String args[]) throws Exception {
      if (args.length !=1){
          System.err.println ("usage: java EchoClient2 &lt;host&gt;");
          System.exit(1);
      }               
      String host = args[0];
      int port = 6500;
      String cmd, line;
      Socket socket = new Socket(host,port);
      BufferedReader input = new BufferedReader(
              new InputStreamReader(socket.getInputStream()));
      PrintStream output = new PrintStream(socket.getOutputStream(),true);
  
          while( true ) {//begin cycle
  
              Scanner scan = new Scanner (System.in);
  
              System.out.println("Begin Cycle");//For debugging
  
              if (input.readLine().equals("***BLACKLISTED***")) {
                  System.out.println("IP is Blacklisted");
                  break;
              }
  
              System.out.println("Continue Cycle");//For debugging
  
              System.out.println(" ");
              System.out.println("CLIENT MENU");
              System.out.println(" ");
              System.out.println("1 - List on-line users");
              System.out.println("2 - Send message to single user");
              System.out.println("3 - Send message to all users");
              System.out.println("4 - Show Whitelist");
              System.out.println("5 - Show Blacklist");
              System.out.println("9 - Exit");
              System.out.println(" ");
              System.out.print(host+":"+port+"#&gt;"); //Command line            
              cmd = scan.nextLine();// Read command to send   
              output.println(cmd); //Send command to the server
  
              if ( cmd.equalsIgnoreCase("quit")){
                  System.out.println("exiting..");
                  break;//Exit the cycle
              }           
              while (!(line = input.readLine()).equals("***CLOSE***")) {//Input cycle                         
              System.out.println (line);//Prints server answer        
              }
  
          }//End of cycle
  
      System.out.println("Connection Terminated");
      input.close();//Close input
      output.close();//Close output
      socket.close();//Close socket   
  }
  }
  </code></pre>
  
  <p>So the Blacklisted IP's don't reach the <code>t.start()</code> and non-blacklisted IP's, successfully start a new thread. No problem there.</p>
  
  <p>The problem is on the Client side, a non-blacklisted IP, begins the cycle and it prints <code>System.out.println("Begin Cycle");</code> but it never reaches <code>System.out.println("Continue Cycle");</code></p>