BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #36823同步于 2014/12/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

[求助]AIO 非阻塞通信 线程池 使用CompletionHandle接收客户端

chenhebing
2014/12/8镜像同步8 回复
大家好,java提供了异步channel实现TCP协议 的通信,但是使用异步channel分组管理器时,要使用CompletionHandler接收客户端连接,运行server端程序时,由于client端还没运行,接收不到客户端连接,又是异步非阻塞得,所以server端程序马上结束退出。后面加Thread.sleep(5000),client在5s内启动可以,但是不能解决根本问题。放while(true)里面会报错,请教大神这个问题应该怎么解决,谢谢!!
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
jkfbrant机器人#1 · 2014/12/8
没用过,但为啥放while true里会报错 你放while true里,然后sleep 试试
wkxfivestars机器人#2 · 2014/12/8
贴代码
chenhebing机器人#3 · 2014/12/8
sleep时间到了依然会抛出异常。。 【 在 jkfbrant 的大作中提到: 】 : 没用过,但为啥放while true里会报错 你放while true里,然后sleep 试试
chenhebing机器人#4 · 2014/12/8
这是server端的 import java.util.*; import java.io.*; import java.nio.*; import java.net.*; import java.nio.channels.*; import java.util.concurrent.*; import java.nio.charset.*; public class AIOServer { static final int PORT=30000; static final String UTF_8="utf-8"; static List<AsynchronousSocketChannel> channelList=new ArrayList<>(); public void startListen() throws InterruptedException,Exception { ExecutorService executor=Executors.newFixedThreadPool(20); AsynchronousChannelGroup channelGroup=AsynchronousChannelGroup.withThreadPool(executor); AsynchronousServerSocketChannel serverChannel=AsynchronousServerSocketChannel.open(channelGroup).bind(new InetSocketAddress(PORT)); // while(true) // { serverChannel.accept(null,new AcceptHandler(serverChannel)); // // Thread.sleep(10000); // } } public static void main(String [] args) throws Exception { AIOServer server=new AIOServer(); server.startListen(); } } class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,Object> { private AsynchronousServerSocketChannel serverChannel; public AcceptHandler(AsynchronousServerSocketChannel sc) { this.serverChannel=sc; } ByteBuffer buff=ByteBuffer.allocate(1024); @Override public void completed(final AsynchronousSocketChannel sc,Object attachment) { AIOServer.channelList.add(sc); serverChannel.accept(null,this); sc.read(buff,null,new CompletionHandler<Integer,Object>() { @Override public void completed(Integer rsult,Object attachment) { buff.flip(); String content=StandardCharsets.UTF_8.decode(buff).toString(); for(AsynchronousSocketChannel c:AIOServer.channelList) { try { c.write(ByteBuffer.wrap(content.getBytes(AIOServer.UTF_8))).get(); } catch (Exception ex) { ex.printStackTrace(); } } buff.clear(); sc.read(buff,null,this); } @Override public void failed(Throwable ex,Object attachment) { System.out.println("读取数据失败:"+ex); AIOServer.channelList.remove(sc); } }); } public void failed(Throwable ex,Object attachment) { System.out.println("连接失败:"+ex); } } 【 在 wkxfivestars 的大作中提到: 】 : 贴代码
doubleke机器人#5 · 2014/12/8
bd 【 在 chenhebing 的大作中提到: 】 : 大家好,java提供了异步channel实现TCP协议 的通信,但是使用异步channel分组管理器时,要使用CompletionHandler接收客户端连接,运行server端程序时,由于client端还没运行,接收不到客户端连接,又是异步非阻塞得,所以server端程序马上结束退出。后面加Thread.sleep(5000),client在5s内启动可以,但是不能解决根本问题。放while(true)里面会报错,请教大神这个问题应该怎么解决,谢谢!! 发自「贵邮」
huhahuhaha机器人#6 · 2014/12/8
bd
wkxfivestars机器人#7 · 2014/12/8
我看这个帖子里有句话: //因为AIO不会阻塞调用进程,因此必须在主进程阻塞,才能保持进程存活。 try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } 不知道能解决你的问题不? 【 在 chenhebing 的大作中提到: 】 : 这是server端的 : import java.util.*; : import java.io.*; : ...................
chenhebing机器人#8 · 2014/12/9
谢谢了!这个可以解决问题的。它相当于sleep了很长时间,直到有连接为止!再谢! 【 在 wkxfivestars 的大作中提到: 】 : 我看这个帖子里有句话: : [code=java] : //因为AIO不会阻塞调用进程,因此必须在主进程阻塞,才能保持进程存活。 : ...................