我在我的 android 应用程序中使用 TCP 连接。连接在专用线程中运行。
我的代码:
class ReceiverThread extends Thread {
Handler handler;
ReceiverThread(Handler h) {
handler = h;
}
@Override
public void run() {
try {
socketTCP = new Socket((SERVERIP), SERVERPORT);
in = new BufferedReader(new InputStreamReader(
socketTCP.getInputStream()));
socketTCP.setSoTimeout(20000);
while (TCPRunning) {
String strTcp = in.readLine().toString();
i++;
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("getStr", strTcp + "\n");
msg.setData(b);
handler.sendMessage(msg);
}
} catch (UnknownHostException e) {
Log.v("TcpClient", "Unknown host");
try {
socketTCP.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} catch (SocketException e) {
// Erreur TimeOut
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("getStr", "Error");
msg.setData(b);
handler.sendMessage(msg);
e.printStackTrace();
} catch (IOException e) {
if (socketTCP != null) {
try {
socketTCP.close();
socketTCP = null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
} finally {
try {
i = 0;
if (in != null) {
in.close();
}
if (socketTCP != null) {
socketTCP.close();
socketTCP = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
然后,当我启动我的应用程序时,我将 android 设备连接到另一台设备的 wifi;它运作良好:我接收到另一台设备发送的数据。当我停止连接并重新启动时(几秒钟后)它可以工作。
但是当我等待 2 或 3 分钟时,连接变得不可能:
我得到以下异常:
02-25 10:40:32.638: W/System.err(4384): java.net.ConnectException: failed to connect to /192.168.56.1 (port 50000): connect failed: EHOSTUNREACH (No route to host)
02-25 10:40:32.648: W/System.err(4384): at libcore.io.IoBridge.connect(IoBridge.java:114)
02-25 10:40:32.648: W/System.err(4384): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-25 10:40:32.648: W/System.err(4384): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
02-25 10:40:32.648: W/System.err(4384): at java.net.Socket.startupSocket(Socket.java:566)
02-25 10:40:32.648: W/System.err(4384): at java.net.Socket.tryAllAddresses(Socket.java:127)
02-25 10:40:32.658: W/System.err(4384): at java.net.Socket.<init>(Socket.java:177)
02-25 10:40:32.658: W/System.err(4384): at java.net.Socket.<init>(Socket.java:149)
02-25 10:40:32.658: W/System.err(4384): at nke.service.wificonnection.ThreadWifi$ReceiverThread.run(ThreadWifi.java:163)
02-25 10:40:32.658: W/System.err(4384): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
02-25 10:40:32.658: W/System.err(4384): at libcore.io.Posix.connect(Native Method)
02-25 10:40:32.658: W/System.err(4384): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
02-25 10:40:32.668: W/System.err(4384): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-25 10:40:32.668: W/System.err(4384): at libcore.io.IoBridge.connect(IoBridge.java:112)
02-25 10:40:32.668: W/System.err(4384): ... 7 more
如果我想再次使用连接,我必须刷新wifi连接(在Android的参数中,停止并重新启动wifi并连接到设备的wifi)。
可能是什么问题呢?错误后关闭套接字时是否有问题?