首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓客户端-服务器应用程序- readLine()不工作

安卓客户端-服务器应用程序- readLine()不工作
EN

Stack Overflow用户
提问于 2014-09-04 01:47:26
回答 1查看 148关注 0票数 0

我正在编写一个基于套接字的客户端(安卓)-server(Java)应用程序。我的问题是我需要在服务器上处理两种类型的消息(MINDWAVE和SPHERO)。mindwave消息被服务器很好地处理了,但我对sphero消息有一个问题:-client将消息"SPHERO“发送到服务器。-server会打印“捕获了Sphero请求”。并仔细检查其其余代码,并在其"while((fromServer = in.readLine())!=null)“循环中驻留(它甚至不会启动循环的第一个操作--只是驻留在-client ()部分)。

客户端的线程

代码语言:javascript
复制
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;
    }

}

和服务器的消息处理:

代码语言:javascript
复制
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();
        }

    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-09-04 03:42:51

readLine()仅在读取换行符或流关闭时返回。所以你应该把"SPHERO\n"发送出去。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25650599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档