2010-08-09 45 views
0

在我的Android應用程序中,我嘗試連接到本地服務器上的端口以獲取一些數據包。我一直包裹到代碼中的一些嘗試&趕上的,但下面的代碼:Android網絡編程:IOExceptions和StackOverflowError

address = "192.168.175.82"; 
public void run() { 
    try { 
    smtpSocket = new Socket(address, 60001); 

     os = new DataOutputStream(smtpSocket.getOutputStream()); 
     is = new DataInputStream(smtpSocket.getInputStream()); 
    } catch (UnknownHostException e) { 
     System.err.println("Don't know about host: " + address); 
     run(); 
    } catch (IOException e) { 
     System.err.println("Couldn't get I/O for the connection to: " + address); 
     run(); 
    } catch (Exception e) { 
    System.out.println("retry"); 
    run(); 
    } 

如存在服務器不在,我明明得到了IOException異常幾次(如幾百次)之前,我收到了堆棧溢出錯誤和應用程序崩潰。

01-20 22:21:32.526: ERROR/AndroidRuntime(5678): java.lang.StackOverflowError 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.util.Hashtable.get(Hashtable.java:282) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.util.Properties.getProperty(Properties.java:177) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.lang.System.getProperty(System.java:440) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.lang.System.getProperty(System.java:412) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.lang.Boolean.getBoolean(Boolean.java:174) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at org.apache.harmony.luni.net.NetUtil$Action.run(NetUtil.java:89) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at org.apache.harmony.luni.net.NetUtil$Action.run(NetUtil.java:80) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.security.AccessController.doPrivilegedImpl(AccessController.java:264) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.security.AccessController.doPrivileged(AccessController.java:84) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at org.apache.harmony.luni.net.NetUtil.preferIPv6Addresses(NetUtil.java:51) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:190) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.net.Socket.startupSocket(Socket.java:777) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.net.Socket.tryAllAddresses(Socket.java:192) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.net.Socket.<init>(Socket.java:256) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at java.net.Socket.<init>(Socket.java:220) 
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):  at com.example.sockets.ReadSocket.run(ReadSocket.java:83) 

任何人都可以請指出我正確的方向如何讓這個工作?也許我應該先以某種方式測試連接(如果有的話,請說怎麼樣?) 感謝

克里斯

回答

2

對於初學者,請放棄遞歸調用。這將擺脫堆棧溢出。 用循環代替它,並在每次不成功的迭代中睡眠

public void run() { 
    boolean connected = false; 
    while(!connected) 
    { 
    try { 
     smtpSocket = new Socket(address, 60001); 

      os = new DataOutputStream(smtpSocket.getOutputStream()); 
      is = new DataInputStream(smtpSocket.getInputStream()); 


     connected = true; 
    } catch (UnknownHostException e) { 
     System.err.println("Don't know about host: " + address); 

    } catch (IOException e) { 
     System.err.println("Couldn't get I/O for the connection to: " + address); 

     } catch (Exception e) { 
     System.out.println("retry"); 

     } 
     if (!connected) 
      Thread.sleep(1000) 
     } 
    } 
+0

非常感謝。剛發佈時就明白了這一點!請原諒我的白癡= P。 – 2010-08-09 11:57:31

0

你可以嘗試isConnected()isBound()您的插座上請檢查是否已連接或不試圖獲得一個前的InputStream。

+0

感謝您的回覆!但是,這樣做仍會導致溢出錯誤。我甚至嘗試完全刪除I/O流,但錯誤仍然存​​在。 – 2010-08-09 11:47:55

+0

2方法返回true ??? ,因爲如果他們不這樣做,你可以改變你的處理方式,或者在重複嘗試連接之前設置一個延遲時間? – Sephy 2010-08-09 12:11:16