简体   繁体   English

Android / Java实现Runnable进入无限循环

[英]Android/Java Implements Runnable enters an infinite loop

I am connecting two Android Devices P2P using the following code. 我正在使用以下代码连接两个Android设备P2P。 The problem is that when the server receives data from client its Thread/Runnable doesn't stop. 问题在于,当服务器从客户端接收数据时,其线程/可运行对象不会停止。

Please suggest me a way to stop/forced this to go in an infinite loop. 请建议我一种方法来阻止/强制此过程无限循环。

Following Is the piece of code I am using for my Server Activity : 以下是我用于服务器活动的代码段:

       public class ServerThread implements Runnable {
            private volatile boolean keepGoing = 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 (true) {
                    // 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();
                                    //finish();

                                    //Intent i = new Intent(ServerActivity.this, 
                                        //ClientActivity.class);
                                    //startActivity(i);
                                    //serverStatus.setText(line);
                                    // do whatever you want to the front end
                                    // this is where you can be creative
                                }
                            });
                        }

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

EDIT: 编辑:

   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) && keepGoing) {
                 Log.d("ServerActivity", line);
                 handler.post(new Runnable() {
                     @Override
                     public void run() {

                         serverStatus.setText(line);


                         Log.d("IP recevied", line);
                         requestStop();
                         //finish();

                         //Intent i = new Intent(ServerActivity.this, 
                             //ClientActivity.class);
                         //startActivity(i);
                         //serverStatus.setText(line);
                         // do whatever you want to the front end
                         // this is where you can be creative
                     }
                 });
             }

I think the problem is that even though you signal the server to stop, it's still stuck in the call to serverSocket.accept(); 我认为问题在于,即使您发出信号通知服务器停止,它仍然停留在对serverSocket.accept();的调用中serverSocket.accept(); , waiting for other clients to connect. ,等待其他客户端连接。 What you should do is also close the socket in the stop method: 您还应该在stop方法中关闭套接字:

public void requestStop() {
      keepGoing = false;
      serverSocket.close();
}

to force the code to throw an IOException and then recheck the while condition and exit. 强制代码引发IOException ,然后重新检查while条件并退出。

Why did you comment the line 你为什么评论这句话

  //finish();

once the connection is established? 一旦建立连接?

Since you never finish once you are connected you have an infinite loop 由于一旦连接就永远无法完成,便会陷入无限循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM