第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用Netty Demo報(bào)錯(cuò)

使用Netty Demo報(bào)錯(cuò)

陪伴而非守候 2019-03-01 11:10:48
public class TimeServer { public void bind(int port) { try { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel arg0) throws Exception { System.out.println("初始化"); arg0.pipeline().addLast(new TimeHandler()); } }); ChannelFuture future = b.bind(port).sync(); System.out.println("執(zhí)行這里"); future.channel().closeFuture().sync(); System.out.println("執(zhí)行這里"); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { new TimeServer().bind(10000); } } public class TimeHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); ByteBuf buf=(ByteBuf) msg; //// byte[] butfs = buf.array();//報(bào)錯(cuò) System.out.println(buf.readableBytes()); byte[] butfs = new byte[buf.readableBytes()]; buf.readBytes(butfs); System.out.println(new String(butfs,"UTF-8")); System.out.println(msg); } } 客戶端使用的是BIO的模型: public static void main(String[] args) throws Exception { final int port = 10000; // NioServer server = new NioServer(port); // server.init(); /// ======================================================== // 接下來(lái)模擬3個(gè)Client并發(fā)訪問(wèn)服務(wù)器 int poolsize = 1; ExecutorService pool = Executors.newFixedThreadPool(poolsize); Collection<Callable> tasks = new ArrayList<Callable>(10); final String clientname = "clientThread"; for (int i = 0; i < poolsize; i++) { final int n = i; // 若每一個(gè)Client都保持使用BIO方式發(fā)送數(shù)據(jù)到Server,并讀取數(shù)據(jù)。 tasks.add(new Callable() { @Override public Object call() throws Exception { Socket socket = new Socket("127.0.0.1", port); final InputStream input = socket.getInputStream(); final OutputStream out = socket.getOutputStream(); final String clientname_n = clientname + "_" + n; // BIO讀取數(shù)據(jù)線程 new Thread(clientname_n + "_read") { @Override public void run() { byte[] bs = new byte[1024]; while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } int len = 0; try { while ((len = input.read(bs)) != -1) { System.out.println("Clinet thread " + Thread.currentThread().getName() + " read: " + new String(bs, 0, len)); } } catch (IOException e) { e.printStackTrace(); } } } }.start(); // BIO寫數(shù)據(jù)線程 new Thread(clientname_n + "_write") { @Override public void run() { int a = 0; while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } String str = Thread.currentThread().getName() + " hello, " + a; try { out.write(str.getBytes()); out.flush(); a++; } catch (IOException e) { e.printStackTrace(); } } } }.start(); return null; } }); } pool.invokeAll((Collection<? extends Callable<Object>>) tasks); // server.go(); } 結(jié)果運(yùn)行的時(shí)候出現(xiàn)了以下錯(cuò)誤: 月 13, 2017 5:52:56 下午 io.netty.channel.DefaultChannelPipeline onUnhandledInboundException 警告: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception. io.netty.util.IllegalReferenceCountException: refCnt: 0 at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1408) at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1394) at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1383) at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:850) at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:858) at test.netty.TimeHandler.channelRead(TimeHandler.java:17) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:624) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:559) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:476) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) at java.lang.Thread.run(Thread.java:745) 請(qǐng)問(wèn)這是為什么呢?
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 684 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)