jsfrocha
jsfrocha

Reputation: 1860

Java : Client-Server Simple Application with IP Blacklist

I'm writing a client-server application in simple java (java.net* and java.io*) 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.

The blacklist is working for the IPs that need to be blacklisted and for those it won't even start a thread.

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:

Server:

        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();

And the Client:

public static void main(String args[]) throws Exception {
    if (args.length !=1){
        System.err.println ("usage: java EchoClient2 <host>");
        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+"#>"); //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   
}
}

So the Blacklisted IP's don't reach the t.start() and non-blacklisted IP's, successfully start a new thread. No problem there.

The problem is on the Client side, a non-blacklisted IP, begins the cycle and it prints System.out.println("Begin Cycle"); but it never reaches System.out.println("Continue Cycle");

Upvotes: 0

Views: 1382

Answers (1)

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11607

if (input.readLine().equals("***BLACKLISTED***")) { // Client

readLine is blocking, it waits for a complete line. This is executed in every case. You write to the client only in case of a blacklisted client:

checkBlack.println("***BLACKLISTED***"); // Server

But you don't send any feedback in case of an approved client. Easiest solution would be to just send an approval message.

Upvotes: 2

Related Questions