Java:具有IP黑名单的客户端 - 服务器简单应用程序

时间:2013-06-16 13:56:40

标签: java sockets networking client-server java-io

我正在使用多线程在简单的java(java.net*java.io*)中编写客户端 - 服务器应用程序,以实现多个客户端与服务器的连接。我还试图在文本文件中实现一个简单的IP黑名单,当有人连接到服务器套接字时可以访问它,如果有IP则关闭连接,或者如果不存在则让客户端线程启动。

黑名单正在为需要列入黑名单的IP工作,对于那些甚至不会启动线程的人。

这里的问题是非黑名单的IP,即使它们启动新线程并建立连接,也不会到达应用程序的输入部分:

服务器:

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

客户:

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   
}
}

因此,黑名单IP未到达t.start()和未列入黑名单的IP,成功启动新线程。没问题。

问题在于客户端,非黑名单的IP,开始循环并打印System.out.println("Begin Cycle");但它永远不会到达System.out.println("Continue Cycle");

1 个答案:

答案 0 :(得分:2)

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

readLine正在阻止,它等待完整的一行。这在每种情况下都会执行。只有在列入黑名单的客户端时才写入客户端:

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

但是,如果是经批准的客户,您不会发送任何反馈。最简单的解决方案就是发送批准信息。

相关问题