在我的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
我不明白的问题是什么?
DNS 解析甚至在connect()
调用之前就可能被阻止。将new InetSocketAddress(mWsHost, mWsPort)
放在单独的行上,并打印出其结果。很可能你被困在那里。
如果不是这种情况,请检查是否可以使用 telnet 连接到目标主机/端口:
~$ telnet host port
此问题已解决。我的 wifi 连接无法访问私人资源。但是我如何对网络套接字有其他问题。是的安卓套接字读取方法返回 -1