2014-09-03 2 views
0

Socket을 기반으로 클라이언트 (android) -server (java) 앱을 작성하고 있습니다. 내 문제는 서버에서 두 가지 유형의 메시지 (MINDWAVE 및 SPHERO)를 처리해야한다는 것입니다. mindwave 메시지는 서버에 의해 잘 처리되지만 sphero 하나에 문제가 있습니다. -client는 "SPHERO"메시지를 보내십시오. -server는 "Sphero 요청이 중단되었습니다." (루프의 첫 번째 작업을 시작하지 않습니다 - 그냥 readline에 붙어있다 ((에서 fromServerLoad() =!) 루프 ") 부분).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

답변

1

readLine() 만 반환합니다. 따라서 "SPHERO\n"을 보내야합니다.

관련 문제