2

In my Android project I implemented a Faye client. But, when I call SocketChannel.socket().connect(...), the connection hangs and the next line does not run. It is as if I don`t set the timeout, or disconnect for timeout.

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();
}

If I did so:

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

SocketChannel .isConnected() return false

What is the problem I don't understand?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
JULI
  • 126
  • 5

2 Answers2

0

DNS resolution might be blocking even before the connect() call. Put new InetSocketAddress(mWsHost, mWsPort) on a separate line, and print out its results. Chances are you are stuck there.

If that's not the case, check if you can connect to the target host/port with telnet:

~$ telnet host port
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • InetSocketAddress socketAdd = new InetSocketAddress("172.20.71.4", 9292); boolean resolve = socketAdd.isUnresolved(); in socketAdd host null and port 9292, why? and resolve false – JULI Sep 21 '12 at 16:29
  • Try `telnet 172.20.71.4 9292` on the command line. Does that work? – Nikolai Fetissov Sep 21 '12 at 16:37
0

This problem is resolved. My wifi connection didn`t have access to private resource. But how I have other problem with web socket. It is Android socket read method return -1

Community
  • 1
  • 1
JULI
  • 126
  • 5