2011-11-28 86 views
0

我用從服務器到我的Android應用程序通信問題,basiclly通信是這樣的:爲什麼不讀第二行?

1. send x and y 
2. get the list of coordinates from server 
3. send "ok" 
4. receive titles string 

一切進展順利,直到最後一行,我知道這是正常發送,因爲它通過telnet工作,但我的Android應用程序沒有收到任何東西。下面是用於發送和接收客戶端代碼:

clientSocket = new Socket("10.0.2.2", 1234); 
    Log.d("LatitudeE6", ""+point.getLatitudeE6()); 
    Log.d("LongitudeE6", ""+point.getLongitudeE6()); 
    os = new PrintStream(clientSocket.getOutputStream()); 
    is = new DataInputStream(clientSocket.getInputStream()); 
    sentenceX = ""+point.getLatitudeE6(); 
    sentenceY = ""+point.getLongitudeE6(); 
    os.println(sentenceX + " "+ sentenceY+'\n'); 

    String thing =is.readLine(); 
    String [] holder = thing.split("\\s+"); 
    System.out.println("thing printed: " +thing); 

    os.println("ok\n"); 
    String test = is.readLine(); 
    System.out.println("tester: " +test); // this doesn' work 

服務器:

try{ 
    in = new DataInputStream(clientSocket.getInputStream()); 
    os = new PrintStream(clientSocket.getOutputStream()); 
     ms.connector(); 
     while(true){ 
     line = in.readLine(); 

     if(line.startsWith("51789181 19426953")==true){ 
// these are the coordinates, if the client sends them, it will receive the list of other coordinates 
     os.println(ms.lister().toString().replace('[', ' ').replace(']', ' ').trim().replace(',', ' ') +""+'\n'); 
     os.flush(); 

     }else if (line.equals("ok")==true) { 
        os.flush(); 
         os.println(ms.topicDesc().toString().replace('[', ' ').replace(']', ' ').trim().replace(',', ' ') +""+'\n'); 
       } 
    } 
catch(IOException e){ 
      e.printStackTrace(); 
     } 
+3

您發送後確定換行,並刷新插座,在服務器端? –

+0

我剛剛加入「\ n」後「確定」,並刷新插座,但它不會改變任何事情。 – iie

+0

對不起,我miswrote我的評論之前 - 在另一邊的標題字符串後,你發送一個換行符?請顯示服務器代碼。 –

回答

1

您發送兩個換行符,一個通過os.println(),並通過"\n"其他。這轉換爲兩行od數據。在客戶端,您只需通過readLine()閱讀一行。你在服務器和客戶端多次執行此操作。

嘗試從客戶端和服務器上的發送代碼移除"\n"

+0

http://pl.memgenerator.pl/mem-image/u-saved-my - 生命時間賜-PL-FFFFFF – iie

相關問題