0

我正在用简单的 java ( java.net*and 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");

4

1 回答 1

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

readLine正在阻塞,它等待一个完整的行。这在每种情况下都会执行。只有在被列入黑名单的客户的情况下,您才写信给客户:

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

但是,如果客户获得批准,您不会发送任何反馈。最简单的解决方案是只发送一条批准消息。

于 2013-06-16T14:08:38.420 回答