2013-07-21 420 views
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) { 
      } 
     } 
    } 

我一直在使用 而(br.ready()),或在(br.readLine()的isEmpty()),但我不能讓它等待

回答

0

嘗試嘗試使用一段時間(br.readline()!= NULL)

+0

要評論 – liyakat

+0

嗨它不解決我不得不放棄從客戶端2輸入才從服務器獲取響應的問題,它的輸入錯誤或魔字 – AAB

相關問題