虽然他无法连接套接字不抛出异常 [英] Socket does not throw an exception though he can’t connect

查看:130
本文介绍了虽然他无法连接套接字不抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将套接字连接到一个不存在的服务器,我真的不明白为什么一个例外是 不可以 的被提出。

I'm trying to connect a socket to an non-existent server, and I really don't understand why an exception is not being raised.

下面是我的code:

public class TestSocket extends Activity {
    private static final String TAG = "TestSocket";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BasicThread t = new BasicThread();
        t.start();
    }

    class BasicThread extends Thread {
        @Override
        public void run() {
            Log.d(TAG, "Before");
            try {
                new Socket("42.42.42.42", 12345);
                Log.d(TAG, "Connected");
            } catch (Exception e) {
                Log.d(TAG, "Exception");
            }
            Log.d(TAG, "After");
        }
    }
}

我也试过用我自己的IP地址,同时运行Wireshark的,我第一次获得 [SYN] 从Android的我的电脑,然后 [RST ,ACK] 从我的计算机到Android(因为没有在此端口监听),但我仍然没有在Android上得到一个异常。

I also tried with my own IP address while running Wireshark, and I first get [SYN] from Android to my computer and then [RST, ACK] from my computer to Android (because nothing is listening at this port), but I still do not get an exception on Android.

另外我测试物理的手机(Nexus S的),并我有我的清单互联网的权限。

Also I’m testing on a physical phone (Nexus S), and I do have the internet permission in my Manifest.

为什么不我得到一个例外

编辑:

更多precisely,输出我得到的是

More precisely, the output I get is

D/TestSocket(17745): Before
D/TestSocket(17745): Connected
D/TestSocket(17745): After

(而不是例外

推荐答案

在插座的构造函数,当无法确定主机的IP地址,它抛出,所以我认为,因为你不是通过一个主机名这需要的分辨率,不同的异常会得到抛出来代替。我相信例外实际上来自URL类或如由它来完成解析,并从别的地方。

In the Socket constructor, it's thrown when the IP address of the host can't be determined, so I assume that because you aren't passing a hostname which needs resolution, a different exception would be getting thrown instead. I believe the exception actually comes from the URL class or such which does the resolution, and from nowhere else.

的连接(..)方法的的抛出一个异常,但没有出现,就像你说的。

The connect(..) method should throw an exception but doesn't appear to, as you say.

编辑:显然的Andr​​oid(有的版本)不能正常工作在这里,所以它可能是一个错误:的 HTTP://$c$c.google.com/p/android/issues/detail ID = 6144 ?它看起来并不像链接指向,因为我原以为仿真器。

apparently Android (some versions) doesn't work properly here, so it's probably a bug: http://code.google.com/p/android/issues/detail?id=6144. It doesn't look like the link refers to the emulator as I had thought.

这篇关于虽然他无法连接套接字不抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆