SocketTimeoutException

SocketTimeoutException

我正在开发一个通过套接字连接连接到服务器以读取和写入数据的应用程序。
我在AsyncTask中使用runInBackground()方法来执行此操作。

我将PC用作服务器,并将Android设备连接到WiFi,以连接到PC。

我想模拟服务器不可用的情况,因此我将设备从WiFi断开并运行代码。

我使用以下代码:

try
{
    socket = new Socket();
    socket.connect(new InetSocketAddress("192.168.1.104", 4444), 10000);
    ...
    ...
}
catch (SocketTimeoutException e)
{
    e.printStackTrace();
}
catch (ClassNotFoundException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
}
catch (Exception e)
{
    e.printStackTrace();
}


如预期的10秒钟后,抛出SocketTimeoutException,但问题是没有任何catch块实际捕获异常。那怎么可能?

这是日志:

11-09 17:57:15.286: W/System.err(12050): java.net.SocketTimeoutException: failed to connect to     /192.168.1.104 (port 4444) after 10000ms
11-09 17:57:15.301: W/System.err(12050):    at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
11-09 17:57:15.301: W/System.err(12050):    at libcore.io.IoBridge.connect(IoBridge.java:112)
11-09 17:57:15.301: W/System.err(12050):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-09 17:57:15.301: W/System.err(12050):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-09 17:57:15.306: W/System.err(12050):    at java.net.Socket.connect(Socket.java:842)
11-09 17:57:15.306: W/System.err(12050):    at com.elyakimsportal.communication.ContactServer$RetrieveProfilesTask.doInBackground(ContactServer.java:183)
11-09 17:57:15.306: W/System.err(12050):    at com.elyakimsportal.communication.ContactServer$RetrieveProfilesTask.doInBackground(ContactServer.java:1)
11-09 17:57:15.311: W/System.err(12050):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-09 17:57:15.311: W/System.err(12050):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-09 17:57:15.311: W/System.err(12050):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-09 17:57:15.311: W/System.err(12050):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-09 17:57:15.316: W/System.err(12050):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-09 17:57:15.316: W/System.err(12050):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-09 17:57:15.316: W/System.err(12050):    at java.lang.Thread.run(Thread.java:856)
11-09 17:57:15.316: I/System.out(12050): closed connection to server


另外,我查看了Socket.connect(InetSocketAddress,timeout)方法的文档,它没有说它曾经抛出SocketTimeoutException,因此更加混乱。

Parameters
remoteAddr  the address and port of the remote host to connect to.
timeout  the timeout value in milliseconds or 0 for an infinite timeout.

Throws
IllegalArgumentException  if the given SocketAddress is invalid or not supported or the timeout value is negative.
IOException  if the socket is already connected or an error occurs while connecting.


顺便说一句,我的应用程序不会崩溃。我是否可以假设那是因为它在不同的线程中?

最佳答案

在我看来,您提供的示例代码中正在捕获SocketTimeoutException。因为您说您的应用程序不会崩溃,所以这很有道理。

由于您正在使用e.printStackTrace()将堆栈轨道打印到终端,因此它仍可能看起来还是崩溃或未捕获到异常。

如果您不想用这些异常填充日志,请考虑重写:

try {
    socket = new Socket();
    socket.connect(new InetSocketAddress("192.168.1.104", 4444), 10000);
    ...
    ...

} catch (SocketTimeoutException e) {
    System.err.println("SocketTimeoutException: " + e.getMessage());

} catch (ClassNotFoundException e) {
    System.err.println("ClassNotFoundException: " + e.getMessage());

} catch (IOException e) {
    System.err.println("IOException: " + e.getMessage());

} catch (Exception e) {
    System.err.println("Exception: " + e.getMessage());

}

09-28 01:58