2012-09-23 44 views
0

我使用以下代碼連接兩個Android設備P2P。問題是,當服務器從客戶端接收數據時,其Thread/Runable不會停止。Android/Java實現Runable進入無限循環

請建議我一種方法來阻止/強迫它進入無限循環。

下面是一段代碼,我用我的服務器活動時間:

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

編輯:

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 
        } 
       }); 
      } 
+1

將'while(true)'改爲'while(keepGoing)'? – hmjd

+0

它仍然沒有工作:( –

回答

3

我認爲問題是,即使你信號的服務器停止,它仍然停留在調用serverSocket.accept();,等待其他客戶端連接。你應該做的也是關閉套接字在停止方法:

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

迫使代碼拋出一個IOException,然後重新檢查while條件和退出。

+0

是否也會關閉與連接的服務器/客戶端進一步通信的套接字? – user387184

+0

@ user387184:服務器套接字不傳送任何東西。它只接受客戶端連接並創建相應的本地套接字對象(accept的結果)。當然,你以後將無法再使用它,但那麼這就是停止服務器的關鍵所在? – Tudor

+0

噢,是的,你抓住了這個;好的一個人 謝謝 –

0

爲什麼你對此有何評論線

//finish(); 

一旦連接成立?

因爲你永遠不結束,一旦你連接你有一個無限循環

+0

它沒有任何區別,如果我完成活動線程仍然運行......並顯示在logcat –

+0

添加行:keepGoing = false之前完成並更改爲hmjd它顯示「IP received」? – user387184

+0

是的它顯示IP在無限循環中收到 –