2011-11-28 90 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

相关问题