Android实现Runnable没有终止

时间:2012-09-23 10:37:15

标签: java android android-intent

我使用this Tutorial建立客户端服务器连接。 我面临的问题是它不会停止,Log cat继续显示客户端在无限循环中发送数据。此外,服务器在无限循环中接收此数据

这是我的code

任何建议/更改都将受到赞赏,我被困在这个问题,昨天coud'nt罚款一个良好的工作解决方案谷歌。

感谢

修改

客户端代码

    public class ClientThread implements Runnable {


    public void run() {
        try {

            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            socket = new Socket(serverAddr, 8080);
            connected = true;
            while (connected) {
                try {
                    Log.d("ClientActivity", "C: Sending command.");
                    PrintWriter out = new PrintWriter(new BufferedWriter(new 
                     OutputStreamWriter(socket
                                .getOutputStream())), true);
                        // where you issue the commands

                        out.println(serverIpAddress);
                        //connected = false;
                        //socket.close();
                        Log.d("ClientActivity", "C: Sent.");

                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }


            }
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}

服务器端

      public class ServerThread implements Runnable {
    private volatile boolean keepGoing = true;
    private volatile boolean firstLoop = true;

    public void requestStop() {
        keepGoing = false;
      }
    public void run() {
        try {
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                while (keepGoing) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new 
                       InputStreamReader(client.getInputStream()));
                        line = null;
                        while ((line = in.readLine()) != null) {
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    serverStatus.setText(line);
                                    Log.d("IP recevied", line);
                                    requestStop();

                                }
                            });
                        }

                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted.
                        Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

取消注释行//connected = false;应该可以正常工作。

写入流操作后,你应该退出而不是写回同样的东西。