为什么不读二线?

时间:2011-11-28 12:35:18

标签: java android network-programming

我遇到从服务器到我的Android应用程序的通信问题,基本上这样的通信是这样的:

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();
        }

1 个答案:

答案 0 :(得分:1)

您要发送两个换行符,一个来自os.println(),另一个来自"\n"。这转换为两行数据。在客户端,您只需通过readLine()阅读一行。你可以在服务器和客户端多次这样做。

尝试从客户端和服务器上的发送代码中删除"\n"