2011-09-01 91 views
0

我現在面臨這個當前的問題。java.net.ConnectException:/192.168.1.106:8002 - 連接被拒絕(Android例外)

我能夠發送命令到設備並從設備接收來自android模擬器的響應到套接字。

但是,當我在平板電腦上安裝相同的應用程序時,出現問題。我第一次發送命令來檢查設備連接與否的狀態,它向我發送設備連接的響應,但是當我第二次發送命令時,它會拋出以下異常:

java.net.ConnectException: /192.168.1.106:8002 - 連接被拒絕。

這是做請求的代碼:

public static String sendRequestandResponse(final String host,final int port, 
      final String command, 
      final int timeoutInMillis,final int responseLength) throws UnknownHostException,NetworkSettingException 

      { 
     if (host == null) 
     { 
      throw new NullPointerException("host is null"); //NOPMD 
     } 
     Socket clientSocket=null; 
     try { 

      /** 
      * Creating socket connection with IP address and port number to send Command 
      */ 
      try{ 
       clientSocket = new Socket(); 

       SocketAddress remoteAdr = new InetSocketAddress(host, port); 
       clientSocket.connect(remoteAdr, 1000); 
       clientSocket.setSoTimeout(timeoutInMillis); 
      }catch (Exception e) { 
       e.printStackTrace(); 
       throw new NetworkSettingException(e.getMessage()); 
      } 




      final PrintWriter outPutStream = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), CHARSET)); 
      try 
      { 
       outPutStream.print(command); 
         outPutStream.flush(); 
        BufferedReader responseString = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), CHARSET)); 
       response = new StringBuilder(); 
       try 
       { 

        int pos = 0; 
        while (true) 
        { 
         pos++; 
         System.out.println(pos); 
         int i=responseString.read(); 
         byte[] resp={(byte)i}; 
         System.out.println(new String(resp)); 
         response.append(new String(resp)); 
         if(pos>=responseLength){ 
          { 
           clientSocket.shutdownInput(); 
           clientSocket.shutdownOutput(); 
           clientSocket.close(); 
           Log.d("ConnectionSocket", "Socket closed with break"); 
           break; 
          } 


         } 
        } 

       }catch (Exception e) { 
        e.printStackTrace(); 
       } 
       finally 
       { 
        responseString.close(); 
       } 


      } 
      finally 
      { 
       outPutStream.close(); 
      } 

     } 

     catch(IOException ex){ 
     } 
     catch(NullPointerException ex){ //NOPMD 
     } 

     finally 
     { 
      try { 
       clientSocket.shutdownInput(); 
       clientSocket.shutdownOutput(); 
       clientSocket.close(); 
      } catch (NullPointerException ex) { //NOPMD 
      } catch (IOException ex) { 
      } 
     } 
     return response.toString(); 
      } 

我認爲它不關閉套接字第一次,所以第二次就拒絕連接。

但是,相同的代碼在仿真器上工作。

+0

確保您的選項卡可以連接到提到的機器 –

+0

「連接被拒絕」表示沒有服務器在目標主機上的套接字上偵聽。因此,您的客戶端代碼在這裏沒用。 – Ingo

+0

作爲一個旁註...你的異常處理是可怕的:http://source.android.com/source/code-style.html#java-language-rules – mibollma

回答

0

只有當服務器不接受連接時,纔會收到拒絕連接。

大多數情況下,問題是防火牆阻止任何不可信的傳入連接。

我的應用程序主要在服務器有防火牆阻止它時顯示此消息。

所以你可以在你的應用程序的防火牆中添加例外列表。

相關問題