本文整理汇总了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();
}
}
};
}
示例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();
}
}
};
}
示例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()));
}
示例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;
}
}
示例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);
}
示例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);
}
}
}
示例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);
}
示例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);
}