0

我正在尝试制作一个程序,其中服务器向客户端询问密码,如果错误发送消息,但问题是

我无法让 BufferedReader 等到客户端输入密码

显示消息“输入密码”后,我希望客户端输入密码并执行他的操作,但我无法让 BufferedReader 等到客户端在流中发送任何有效数据

**Client**
package FTP;
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.*;

class Client{
    String input,output,str;
    Socket s;
    BufferedReader br;
    PrintWriter pw;
    Scanner in=new Scanner(System.in);

    Thread read=new Thread(new Runnable(){
        @Override
        public void run() {
        try{
            while(true){
             input=br.readLine();
             System.out.println(input);
             if(input.equals("true"))
                 break;
         }
        }catch(Exception E){}  
      }  
    });

    Thread write=new Thread(new Runnable(){
        @Override
        public void run() {
            while(true){
                output=in.nextLine();
                pw.println(output);
            }
       }   
    });
    public void start()throws Exception{
        s=new Socket("localhost",30000);
        br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
        read.start();
        write.start();
    }
    public static void main(String[] args) {
        Client c=new Client();
        try {
            c.start();

        } catch (Exception e) {
        }
    }

}

服务器

package FTP;
import java.net.*;
import java.io.*;
    class Server{
        String input,output,path="D:/FTP/Server";
        private final String passwd="0000";
        ServerSocket ss;
        Socket s;
        BufferedReader br;
        PrintWriter pw;


        Thread read=new Thread(new Runnable(){
            @Override
            public void run() {
           try{
                while(true){
                input=br.readLine();
              }
            }catch(Exception e){}
          }
        });
        Thread write=new Thread(new Runnable(){
            @Override
            public void run() {

                    while(true){
                    try {
                        pw.println("Enter Password");  
                     //while(br.readLine().isEmpty()){

                     //}

                     if(input.equals(passwd)){
                         pw.println("You Entered Magic Word");
                         pw.println("true");
                         break;
                     }  

                     else{
                         pw.println("Wrong Entry");
                         pw.flush();
                     }
                    } catch (Exception e) {} 
                    }  
               }

        });


        public void start() throws Exception{

            ss=new ServerSocket(30000);
            s=ss.accept();
            br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
            read.start();
            write.start();
        }
        public static void main(String[] args) {
            Server s=new Server();
            try {
                s.start();
            } catch (Exception e) {
            }
        }
    }

我曾尝试使用 while(br.ready()) 或 while(br.readLine().isEmpty()) 但无法让它等待

4

1 回答 1

0

尝试使用 while(br.readline()!=null)

于 2013-07-21T13:23:11.493 回答