本文整理汇总了Java中java.nio.channels.AsynchronousServerSocketChannel类的典型用法代码示例。如果您正苦于以下问题:Java AsynchronousServerSocketChannel类的具体用法?Java AsynchronousServerSocketChannel怎么用?Java AsynchronousServerSocketChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AsynchronousServerSocketChannel类属于java.nio.channels包,在下文中一共展示了AsynchronousServerSocketChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listen
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
@Override
public void listen(int thread, int port, AioServerListener listener) {
this.port = port;
this.listener = listener;
try {
channelGroup = AsynchronousChannelGroup.withFixedThreadPool(thread, Executors.defaultThreadFactory());
serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup);
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.accept(null, this);
if (logger.isInfoEnable())
logger.info("启动AIO监听[{}]服务。", port);
} catch (IOException e) {
logger.warn(e, "启动AIO监听[{}]服务时发生异常!", port);
}
}
示例2: 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();
}
}
};
}
示例3: 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();
}
}
};
}
示例4: listen
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
@Override
protected void listen(int backlog) throws IOException {
AsynchronousServerSocketChannel channel = AsynchronousServerSocketChannel.open();
try {
if (localAddress != null) {
channel.bind(new InetSocketAddress(localAddress, localport), backlog);
localport = ((InetSocketAddress) channel.getLocalAddress()).getPort();
}
} catch (IOException e) {
try {
channel.close();
} catch (IOException suppressed) {
e.addSuppressed(suppressed);
}
throw e;
}
this.channel = channel;
}
示例5: 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()));
}
示例6: startServer
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
public synchronized void startServer(SocketAddress addr, Object attachment, AsynchronousChannelGroup group)
{
stopServer();
try
{
_acceptor = AsynchronousServerSocketChannel.open(group);
int backlog = onAcceptorCreated(_acceptor, attachment);
if(backlog >= 0)
{
_acceptor.bind(addr, backlog);
beginAccept();
return;
}
}
catch(Throwable e)
{
doException(null, e);
}
stopServer();
}
示例7: run
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
@Override
public void run() {
try {
final int workerThreads = Math.max(4, 2 *
Runtime.getRuntime().availableProcessors());
channelGroup = AsynchronousChannelGroup.withFixedThreadPool(
workerThreads, Executors.defaultThreadFactory());
serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup);
serverSocketChannel.bind(new InetSocketAddress(port));
serverSocketChannel.accept(null,
new AcceptCompletionHandler(serverSocketChannel));
channelGroup.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} catch(IOException | InterruptedException ex) {
Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
}
示例8: 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;
}
}
示例9: AIOAcceptor
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
public AIOAcceptor(String name, String ip, int port,
FrontendConnectionFactory factory, AsynchronousChannelGroup group)
throws IOException {
this.name = name;
this.port = port;
this.factory = factory;
serverChannel = AsynchronousServerSocketChannel.open(group);
/** 设置TCP属性 */
serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 1024 * 16 * 2);
// backlog=100
serverChannel.bind(new InetSocketAddress(ip, port), 100);
}
示例10: ServerSocketRule
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
public ServerSocketRule() {
try {
listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
address = (InetSocketAddress) listener.getLocalAddress();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例11: AsyncTimeServerHandler
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
public AsyncTimeServerHandler(int port){
this.port = port;
try{
//创建AsyncTimeServerHandler并启动服务端Socket
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();
asynchronousServerSocketChannel.bind(new InetSocketAddress(this.port));
System.out.println("服务器已启动, 端口号: " + port);
}catch(Exception e){
e.printStackTrace();
}
}
示例12: AsyncServerHandler
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
public AsyncServerHandler(int port) {
try {
// 创建服务端通道
channel = AsynchronousServerSocketChannel.open();
// 绑定端口
channel.bind(new InetSocketAddress(port));
System.out.println("服务器已启动,端口号:" + port);
} catch (IOException e) {
e.printStackTrace();
}
}
示例13: createListener
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
private AsynchronousServerSocketChannel createListener(AsynchronousChannelGroup channelGroup) throws IOException {
final AsynchronousServerSocketChannel listener = openChannel(channelGroup);
listener.setOption(StandardSocketOptions.SO_REUSEADDR, true);
listener.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024);
listener.bind(new InetSocketAddress(port), 0);
return listener;
}
示例14: start
import java.nio.channels.AsynchronousServerSocketChannel; //导入依赖的package包/类
public void start(String serverIp, int serverPort) throws IOException
{
this.serverNode = new Node(serverIp, serverPort);
ExecutorService groupExecutor = serverGroupContext.getGroupExecutor();
AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withThreadPool(groupExecutor);
serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup);
serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
serverSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 64 * 1024);
InetSocketAddress listenAddress = null;
if (StringUtils.isBlank(serverIp))
{
listenAddress = new InetSocketAddress(serverPort);
} else
{
listenAddress = new InetSocketAddress(serverIp, serverPort);
}
serverSocketChannel.bind(listenAddress, 0);
AcceptCompletionHandler<SessionContext, P, R> acceptCompletionHandler = serverGroupContext.getAcceptCompletionHandler();
serverSocketChannel.accept(this, acceptCompletionHandler);
System.out.println("start server on " + this.serverNode);
}
示例15: 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);
}