在我的Android应用中,我尝试连接到本地服务器上的端口以获取一些数据包。我已经尝试了一些代码和代码。赶上,但使用以下代码:
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)
有谁能请我指出如何使其发挥作用的正确方向?也许我应该先以某种方式测试连接(如果是这样,请说明怎么做?) 感谢
克里斯
答案 0 :(得分: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)
}
}
答案 1 :(得分:0)
您可以在套接字上尝试isConnected()
和isBound()
,以便在尝试获取输入流之前检查您是否已连接。