2014-09-03 129 views
0

我正在編寫一個基於Socket的客戶端(android)服務器(java)應用程序。我的問題是我需要在服務器上處理兩種類型的消息(MINDWAVE和SPHERO)。 mindwave消息由服務器處理得很好,但是我遇到了sphero問題: -client發送消息「SPHERO」來斷開連接。 - 服務器打印「Sphero請求抓取」。並通過其餘代碼罰款 - 客戶端在其「while((fromServer = in.readLine())!= null)」循環(它甚至不啓動循環的第一個操作 - 只是在readline )部分)。Android客戶端服務器應用程序 - readLine()不起作用

客戶的線程

class SendSpheroRequest extends AsyncTask<Void, Void, Void> { 

    String fromServer = ""; 
    int movement; 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     while (isActive) { 
      try { 
       socket = new Socket(address, port); 
       Thread.sleep(1000); 

       out = new PrintWriter(socket.getOutputStream(), true); 

       out.write(TAG); 
       out.flush(); 
       out.close(); 

       socket = new Socket(address, port); 
       in = new BufferedReader(new InputStreamReader(
         socket.getInputStream())); 
       while ((fromServer = in.readLine()) != null) { 
        Toast.makeText(getApplicationContext(), fromServer, 
          Toast.LENGTH_SHORT).show(); 
        if (!fromServer.equalsIgnoreCase("")) { 
         try { 
          movement = Integer.parseInt(fromServer); 

          if (movement > 0) { 
           driveUp(); 
          } else if (movement < 0) { 
           driveDown(); 
          } 
          tvPosition.setText(movement + ""); 
         } catch (Exception e) { 
          e.printStackTrace(); 
          movement = 0; 
         } 
         fromServer = ""; 
        } 
       } 

       Thread.sleep(1000); 

      } catch (UnknownHostException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     try { 
      socket.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

} 

,並且服務器的消息處理:

public void processMessage(String message) { 
    Message messageObject = new Message(message); 

    if (messageObject.getClientType() == DEVICE_TYPE.MINDWAVE) { 
     System.out.println("Message sent by " 
       + messageObject.getClientType() + " with ID=" 
       + messageObject.getClientID() + ". The attention value is " 
       + messageObject.getAttention()); 
     switch (messageObject.getClientID()) { 
     case 1: { 
      if (firstClientIterator < 5 && gameStarted 
        && messageObject.getAttention() != 0) { 
       firstClientAttentionSum += messageObject.getAttention(); 
       firstClientIterator++; 
       System.out.println("sum=" + firstClientAttentionSum 
         + " iterator=" + firstClientIterator); 
      } 
     } 
      break; 
     case 2: { 
      if (secondClientIterator < 5 && gameStarted 
        && messageObject.getAttention() != 0) { 
       secondClientAttentionSum += messageObject.getAttention(); 
       secondClientIterator++; 
       System.out.println("sum=" + secondClientAttentionSum 
         + " iterator=" + secondClientIterator); 
      } 
     } 
      break; 
     default: 
      System.err 
        .println("Cannot process the message. Hint: wrong id detected."); 
     } 
    } else if (messageObject.getClientType() == DEVICE_TYPE.SPHERO) { 
     System.out.println("Sphero request catched."); 
     try { 
      toClientPrintWriter = new PrintWriter(clientSocket.getOutputStream(), true); 
      if (firstClientIterator == 5 && secondClientIterator == 5) { 
       int difference = firstClientAttentionSum 
         - secondClientAttentionSum; 
       System.out.println("Sending data to Sphero. " 
         + "The difference is " + difference + "."); 
       firstClientIterator = secondClientIterator = firstClientAttentionSum = secondClientAttentionSum = 0; 
       toClientPrintWriter.println(difference+""); 

      } else { 
       toClientPrintWriter.println("No results yet."); 
      } 
      toClientPrintWriter.flush(); 
      toClientPrintWriter.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 
+1

爲什麼睡覺?它們實際上是浪費時間。 – EJP 2014-09-03 20:03:45

回答

1

readLine()只有當它讀取一個新行或在流關閉返回。所以你應該發送"SPHERO\n"

相關問題