在我的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)
任何人都可以請指出我正確的方向如何讓這個工作?也許我應該先以某種方式測試連接(如果有的話,請說怎麼樣?) 感謝
克里斯
非常感謝。剛發佈時就明白了這一點!請原諒我的白癡= P。 – 2010-08-09 11:57:31