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


Java AsynchronousServerSocketChannel.accept方法代码示例

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


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

示例1: serverAsyncRunnable

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
public Runnable serverAsyncRunnable(int byteCount, int numClients) {
		return new Runnable() {
			@Override
			public void run() {
				try {
					AsynchronousServerSocketChannel socket = AsynchronousServerSocketChannel.open(asynChanGroupFJP)
							.bind(null);
//					System.out.println("bound");
					SocketAddress localAddress = socket.getLocalAddress();
					List<ForkJoinTask<?>> startClientsThreadAsync = startClientsThreadAsync(localAddress, numClients);
					socket.accept(numClients, acceptCompletionHandler(byteCount, socket));
					for (ForkJoinTask<?> f:startClientsThreadAsync) {
						f.join();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		};
	}
 
开发者ID:arienkock,项目名称:parallelism-benchmarks,代码行数:22,代码来源:FountainSocketBenchmark.java

示例2: acceptCompletionHandler

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
private CompletionHandler<AsynchronousSocketChannel, Integer> acceptCompletionHandler(
			int byteCount, AsynchronousServerSocketChannel socket) {
		return new CompletionHandler<AsynchronousSocketChannel, Integer>() {
			public void completed(
					AsynchronousSocketChannel ch,
					Integer acceptsToGo) {
				acceptsToGo = acceptsToGo-1;
//				System.out.println("server accepted, to go = " + acceptsToGo);
				writeStuffThreadAsync(socket, ch, byteCount, acceptsToGo);
				if (acceptsToGo > 0) {
					socket.accept(acceptsToGo, acceptCompletionHandler(byteCount, socket));
				}
			}
			public void failed(Throwable exc, Integer attachment) {
				exc.printStackTrace();
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
	}
 
开发者ID:arienkock,项目名称:parallelism-benchmarks,代码行数:24,代码来源:FountainSocketBenchmark.java

示例3: main

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, ExecutionException,
          InterruptedException, TimeoutException {
    //打开 AsychronousServerSocketChannel 并将其绑定到类似于 ServerSocketChannel 的地址
    //方法 bind() 将一个套接字地址作为其参数。找到空闲端口的便利方法是传递一个 null 地址,它会自动将套接字绑定到本地主机地址,并使用空闲的 临时 端口。
    AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(null);
    //告诉通道接受一个连接
    Future<AsynchronousSocketChannel> future = server.accept();

//    利用 Future 对象,当前线程可阻塞来等待结果:
    AsynchronousSocketChannel worker = future.get();
    //超时
    //AsynchronousSocketChannel worker = future.get(10, TimeUnit.SECONDS);
    //取消
//    if (!future.isDone()) {
//      future.cancel(true);//cancel() 方法可利用一个布尔标志来指出执行接受的线程是否可被中断
//    }
//
//// read a message from the client
    ByteBuffer readBuffer = ByteBuffer.allocate(1024);
    worker.read(readBuffer).get(10, TimeUnit.SECONDS);
    System.out.println("Message: " + new String(readBuffer.array()));
  }
 
开发者ID:edgar615,项目名称:javase-study,代码行数:23,代码来源:AsynchronousServerSocketMain.java

示例4: start

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
@Override
public void start() throws IOException, InterruptedException {
    if (running.compareAndSet(false, true)) {
        final CountDownLatch latch = new CountDownLatch(1);

        final AsynchronousServerSocketChannel ssc = AsynchronousServerSocketChannel.open();
        ssc.bind(new InetSocketAddress(port));

        ssc.accept(this, new AcceptHandler());

        connectionLatch = latch;
        serverSocketChannel = ssc;
    }
}
 
开发者ID:altiplanogao,项目名称:io-comparison,代码行数:15,代码来源:AioServer.java

示例5: failed

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
/** 
 * @see java.nio.channels.CompletionHandler#failed(java.lang.Throwable, java.lang.Object)
 * 
 * @param exc
 * @param aioServer
 * @重写人: tanyaowu
 * @重写时间: 2016年11月16日 下午1:28:05
 * 
 */
@Override
public void failed(Throwable exc, AioServer<SessionContext, P, R> aioServer)
{
	AsynchronousServerSocketChannel serverSocketChannel = aioServer.getServerSocketChannel();
	serverSocketChannel.accept(aioServer, this);

	log.error("[" + aioServer.getServerNode() + "]监听出现异常", exc);

}
 
开发者ID:tywo45,项目名称:talent-aio,代码行数:19,代码来源:AcceptCompletionHandler.java

示例6: run

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
public void run() {
    while (isRunning) {
        try {
            SocketAddress address = new InetSocketAddress("localhost", mainPort);
            AsynchronousServerSocketChannel channel = AsynchronousServerSocketChannel.open().bind(address);

            channel.accept(null, null);
        } catch(Exception e) {
            System.err.println(e);
        }
    }
}
 
开发者ID:kriskalish,项目名称:hologram,代码行数:13,代码来源:TcpReceiverConnectorNio2.java

示例7: main

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    final AsynchronousServerSocketChannel ssc =
            AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));
    Future<AsynchronousSocketChannel> accepted = ssc.accept();
    AsynchronousSocketChannel sc2 = accepted.get();

    ByteBuffer bb = ByteBuffer.allocateDirect(4096);
    Future<Integer> readFuture = sc2.read(bb);
}
 
开发者ID:lifey,项目名称:compfut,代码行数:10,代码来源:AsyncServer.java

示例8: main

import java.nio.channels.AsynchronousServerSocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    //		AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(4));
    AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open()
            .bind(new InetSocketAddress("0.0.0.0", 8013));

    for (;;) {

        Future<AsynchronousSocketChannel> f = server.accept();

        AsynchronousSocketChannel channel = f.get();

        DebugUtil.debug("client:" + channel.toString());

    }
    //		group.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
}
 
开发者ID:generallycloud,项目名称:baseio,代码行数:17,代码来源:AioServerAccept.java


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