java - Android 客户端-服务器应用程序 - readLine() 不起作用

标签 java android client-server

我正在编写一个基于 Socket 的客户端(android)-服务器(java)应用程序。我的问题是我需要在服务器上处理两种类型的消息(MINDWAVE 和 SPHERO)。服务器很好地处理了 Mindwave 消息,但我在 sphero one 上遇到了问题: - 客户端发送消息“SPHERO”到服务器。 -服务器打印“Sphero 请求已捕获”。并仔细检查其余代码 -client 卡在其“while((fromServer = in.readLine())!=null)”循环上(它甚至没有启动循环的第一个操作 - 只是卡在 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();
        }

    }
}

最佳答案

readLine() 仅在读取换行符或流关闭时返回。因此您应该发送“SPHERO\n”

关于java - Android 客户端-服务器应用程序 - readLine() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650599/

相关文章:

java - 单链表——数据结构逻辑

java - 将枚举的每个数组值映射到枚举值

android - Lint 警告Android

android - 如果在 Android Google Analytics 中不调用 stop() 函数会发生什么?

java - 球线碰撞

java - 引用 java 父类(super class)的属性时使用 groovy 'super' 关键字

安卓工作室 : How to force re-installation (Disable instant run for once)?

c++ - 套接字接收调用卡住线程大约。 5秒

iphone - iPhone在线多人游戏需要什么类型的网络编程?

java - 我缺少类似 : 'isConnected' on ClientDolphin class (open-dolphin) 的内容