当前位置: 首页>>代码示例>>Java>>正文


Java SocketChannel.isBlocking方法代码示例

本文整理汇总了Java中java.nio.channels.SocketChannel.isBlocking方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.isBlocking方法的具体用法?Java SocketChannel.isBlocking怎么用?Java SocketChannel.isBlocking使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.SocketChannel的用法示例。


在下文中一共展示了SocketChannel.isBlocking方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} 
 * with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * 
 * @param channel - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, 
                    SocketAddress endpoint, int timeout) throws IOException {
  
  boolean blockingOn = channel.isBlocking();
  if (blockingOn) {
    channel.configureBlocking(false);
  }
  
  try { 
    if (channel.connect(endpoint)) {
      return;
    }

    long timeoutLeft = timeout;
    long endTime = (timeout > 0) ? (Time.now() + timeout): 0;
    
    while (true) {
      // we might have to call finishConnect() more than once
      // for some channels (with user level protocols)
      
      int ret = selector.select((SelectableChannel)channel, 
                                SelectionKey.OP_CONNECT, timeoutLeft);
      
      if (ret > 0 && channel.finishConnect()) {
        return;
      }
      
      if (ret == 0 ||
          (timeout > 0 &&  
            (timeoutLeft = (endTime - Time.now())) <= 0)) {
        throw new SocketTimeoutException(
                  timeoutExceptionString(channel, timeout, 
                                         SelectionKey.OP_CONNECT));
      }
    }
  } catch (IOException e) {
    // javadoc for SocketChannel.connect() says channel should be closed.
    try {
      channel.close();
    } catch (IOException ignored) {}
    throw e;
  } finally {
    if (blockingOn && channel.isOpen()) {
      channel.configureBlocking(true);
    }
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:58,代码来源:SocketIOWithTimeout.java

示例2: connect

import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
 * The contract is similar to {@link SocketChannel#connect(SocketAddress)} 
 * with a timeout.
 * 
 * @see SocketChannel#connect(SocketAddress)
 * 
 * @param channel - this should be a {@link SelectableChannel}
 * @param endpoint
 * @throws IOException
 */
static void connect(SocketChannel channel, 
                    SocketAddress endpoint, int timeout) throws IOException {
  
  boolean blockingOn = channel.isBlocking();
  if (blockingOn) {
    channel.configureBlocking(false);
  }
  
  try { 
    if (channel.connect(endpoint)) {
      return;
    }

    long timeoutLeft = timeout;
    long endTime = (timeout > 0) ? (System.currentTimeMillis() + timeout): 0;
    
    while (true) {
      // we might have to call finishConnect() more than once
      // for some channels (with user level protocols)
      
      int ret = selector.select((SelectableChannel)channel, 
                                SelectionKey.OP_CONNECT, timeoutLeft);
      
      if (ret > 0 && channel.finishConnect()) {
        return;
      }
      
      if (ret == 0 ||
          (timeout > 0 &&  
            (timeoutLeft = (endTime - System.currentTimeMillis())) <= 0)) {
        throw new SocketTimeoutException(
                  timeoutExceptionString(channel, timeout, 
                                         SelectionKey.OP_CONNECT));
      }
    }
  } catch (IOException e) {
    // javadoc for SocketChannel.connect() says channel should be closed.
    /*try {
      channel.close();
    } catch (IOException ignored) {}*/
    throw e;
  } finally {
    if (blockingOn && channel.isOpen()) {
      channel.configureBlocking(true);
    }
  }
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:58,代码来源:SocketIOWithTimeout.java


注:本文中的java.nio.channels.SocketChannel.isBlocking方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。