2

在我的 Android 项目中,我实现了一个 Faye 客户端。但是,当我打电话时SocketChannel.socket().connect(...),连接挂起并且下一行没有运行。好像我没有设置超时,或者断开超时。

Thread.currentThread().setName("WebSocketConnector");
try {
    // "java.net.SocketException: Address family not supported by protocol"
    System.setProperty("java.net.preferIPv6Addresses", "false");

    mTransportChannel = SocketChannel.open();
    // mTransportChannel.configureBlocking(false);
    // the following will block until connection was established or
    // an error occurred!
    mTransportChannel.socket().connect(
            new InetSocketAddress(mWsHost, mWsPort), mOptions.getSocketConnectTimeout());
    Log.i(TAG, "Socket connected");
    // before doing any data transfer on the socket, set socket
    // options
    mTransportChannel.socket().setSoTimeout(
            mOptions.getSocketReceiveTimeout());
    mTransportChannel.socket().setTcpNoDelay(
            mOptions.getTcpNoDelay());

    return null;
} catch (IOException e) {
    Log.e(TAG, e.getMessage());
    return e.getMessage();
}

如果我这样做了:

mTransportChannel = SocketChannel.open();
mTransportChannel.configureBlocking(false);
mTransportChannel.connect(socketAddress);

SocketChannel .isConnected() return false

我不明白的问题是什么?

4

2 回答 2

0

此问题已解决。我的 wifi 连接无法访问私有资源。但是我对网络套接字还有其他问题。它是 Android 套接字读取方法返回 -1

于 2012-10-01T10:01:52.337 回答
0

DNS 解析可能在调用之前就被阻塞了connect()。放在new InetSocketAddress(mWsHost, mWsPort)一个单独的行上,并打印出它的结果。你很可能被困在那里。

如果不是这种情况,请检查您是否可以使用 telnet 连接到目标主机/端口:

~$ telnet host port
于 2012-09-21T15:39:08.857 回答