2012-09-21 69 views
2

在我的Android項目中,我實現了一個Faye客戶端。但是,當我撥打SocketChannel.socket().connect(...)時,連接掛起,下一行不運行。就好像我不設置超時,或者超時斷開連接。Android連接到WebSocket hang

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 

什麼問題我不明白?

回答

0

甚至在撥打connect()之前,DNS分辨率可能會被阻止。將new InetSocketAddress(mWsHost, mWsPort)放在一個單獨的行上,並打印出結果。你有可能呆在那裏。

如果不是這種情況,請檢查您是否可以通過telnet連接到目標主機/端口:

~$ telnet host port 
+0

InetSocketAddress socketAdd = new InetSocketAddress(「172.20.71.4」,9292); boolean resolve = socketAdd.isUnresolved(); 在socketAdd主機null和端口9292,爲什麼?並解決錯誤 – JULI

+0

在命令行嘗試'telnet 172.20.71.4 9292'。那樣有用嗎? –