为什么在SocketChannel中连接不在非阻塞模式下运行?

时间:2011-03-20 21:03:05

标签: android

我正在尝试向不同的主机打开套接字,因为我在非阻塞模式下使用SocketChannel:

ListIterator iter = hosts.listIterator();
SocketChannel sChannel = null;
while (iter.hasNext()) 
{
    Host host = iter.next();
    sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);
    sChannel.connect(new InetSocketAddress(host, 80));
}

以后我为所有人打电话finishConnect()

问题是connect()似乎阻塞了。

1 个答案:

答案 0 :(得分:0)

我看到了相同的行为(在android上)。无论是否通过...

实例化InetSocketAddress,似乎都会出现这种情况
  1. new InetSocketAddress(String host,int port)
  2. new InetSocketAddress(InetAddress.getByAddress(byte [] ipAddress))
  3. new InetSocketAddress(InetAddress.getByAddress(String host,byte [] ipAddress))
  4. 我看到的堆栈如下......

    InetAddress.gethostbyaddr(byte[]) line: not available [native method]   
    InetAddress.getHostByAddrImpl(byte[]) line: 558 
    Inet4Address(InetAddress).getHostName() line: 352   
    SocketChannelImpl.connect(SocketAddress) line: 259  
    ScannerTargetAuxillaryTest.runBusinessUseCase(InetSocketAddress[]) line: 38 
    ScannerTargetAuxillaryTest.testVersion2() line: 125 
    ScannerTargetAuxillaryTest.main() line: 247 
    TestActivity.onCreate(Bundle) line: 46  
    Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047   
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2466  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2519   
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 123 
    ActivityThread$H.handleMessage(Message) line: 1870  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4370    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 868  
    ZygoteInit.main(String[]) line: 626 
    NativeStart.main(String[]) line: not available [native method]