2014-03-25 190 views
0

好吧,就像標題所說的那樣,我很難理解如何通過輸入和輸出流發送字符串。我正在做一個基本的登錄服務器,並希望能夠點擊「登錄」,並有登錄憑據保存在服務器端。這是我的。通過Java中的套接字發送和接收數據

這是我的日誌的操作事件中的try塊按鈕:

       try { 
           Socket socket = Client.s; 
           OutputStream dOut = Client.s.getOutputStream(); 
           PrintWriter socketOut = new PrintWriter(dOut); 

           username = jTextField1.getText(); 
           password = jPasswordField1.getText(); 

           //System.out.println("Username: " + username); 
           //System.out.println("Password: " + password); 

           socketOut.println(username); 
           socketOut.flush(); 

           socketOut.println(password); 
           socketOut.flush(); 

           socketOut.println("Click"); 
           socketOut.flush(); 


         } catch (Exception e){ 
          e.printStackTrace(); 
         } 

這是我的代碼,這是片面的服務器。我試圖接收和存儲這些數據,但它不工作,我不太清楚爲什麼。我也有幾個沒用的循環,我將最終刪除的(忽略那些):

BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    String str = socketIn.readLine(); 
while (buttonClicked == false){ 
    int done = 0; 
    String messageType = socketIn.toString();  
    while(done == 0) { 

     switch(messageType) 
     { 
     case "username": // Type A 
      username = socketIn.toString(); 
     System.out.println("username: " + username); 
     break; 
     case "password": // Type B 
      password = socketIn.toString(); 
     System.out.println("Password: " + password); 
     break; 
     case "Click": 
      buttonClicked = true; 
      break; 
     } 

請注意,我知道我缺少一個右括號,我只抄我的代碼部分。

+0

客戶端發送3行。第一行是用戶名,第二行是密碼,第三行是「點擊」(不知道第三行是什麼)。因此,服務器應讀取3行:第一個讀取行將是用戶名,第二個讀取行將是密碼,第三個讀取行將是「Click」。看到對稱性?讀一行包括調用readLine(),它返回讀取的行。你的開關沒有意義。在讀者上調用toString()沒有意義。 –

回答

1
BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    String str = socketIn.readLine(); 
while (buttonClicked == false){ 
    int done = 0; 
    String messageType = socketIn.toString();  
    while(done == 0) { 

     switch(messageType) 
     { 
     case "username": // Type A 
      username = socketIn.toString(); 
     System.out.println("username: " + username); 
     break; 
     case "password": // Type B 
      password = socketIn.toString(); 
     System.out.println("Password: " + password); 
     break; 
     case "Click": 
      buttonClicked = true; 
      break; 
     } 

你拿流(socketIn.toString();)的.toString(),你應該使用str

BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    String str = socketIn.readLine(); 
while (buttonClicked == false){ 
    int done = 0; 
    while(done == 0) { 

     switch(str) 
     { 
     case "username": // Type A 
      username = str; 
     System.out.println("username: " + username); 
     break; 
     case "password": // Type B 
      password = str; 
     System.out.println("Password: " + password); 
     break; 
     case "Click": 
      buttonClicked = true; 
      break; 
     } 

但是,無論如何,我認爲這不是你想要的...我的意思是,當他通過「密碼」,密碼爲密碼。

也許你想

BufferedReader socketIn = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    String str = socketIn.readLine(); 
while (buttonClicked == false){ 
    int done = 0; 
    while(done == 0) { 

     switch(str) 
     { 
     case "username": // Type A 
      username = socketIn.readLine(); 
     System.out.println("username: " + username); 
     break; 
     case "password": // Type B 
      password = socketIn.readLine(); 
     System.out.println("Password: " + password); 
     break; 
     case "Click": 
      buttonClicked = true; 
      break; 
     } 

來讀取輸入一個新的生產線。

相關問題