乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      netty: 以默認的ByteBuf作為傳輸數(shù)據(jù)

       印度阿三17 2019-05-29

      client部分代碼:

      //線程
      		EventLoopGroup worker = new NioEventLoopGroup();
      		//輔助類
      		Bootstrap b = new Bootstrap();
      		//注冊server
      		b.group(worker)
      		.channel(NioSocketChannel.class)
      		.handler(new ChannelInitializer<SocketChannel>() {
      
      			@Override
      			protected void initChannel(SocketChannel sc) throws Exception {
      				// TODO Auto-generated method stub
      				sc.pipeline().addLast(new ClientHandler());
      			}
      		});
      

        

      clientHandler部分代碼:

      @Override
      	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      		// TODO Auto-generated method stub
      		try {
      			ByteBuf buf = (ByteBuf)msg;
      			byte[] bytes = new byte[buf.readableBytes()];
      			buf.readBytes(bytes);
      			String result = new String(bytes, "utf-8");
      			System.out.println("Server: "   result);
      		}finally {
      			ReferenceCountUtil.release(msg);
      		}
      		
      	}
      

        

      ?

      ?

      下面查看完整代碼 :

      client:

      public static void main(String[] args) throws InterruptedException {
      		
      		//線程
      		EventLoopGroup worker = new NioEventLoopGroup();
      		//輔助類
      		Bootstrap b = new Bootstrap();
      		//注冊server
      		b.group(worker)
      		.channel(NioSocketChannel.class)
      		.handler(new ChannelInitializer<SocketChannel>() {
      
      			@Override
      			protected void initChannel(SocketChannel sc) throws Exception {
      				// TODO Auto-generated method stub
                                     //不做任何處理,ByteBuf格式傳輸
      				sc.pipeline().addLast(new ClientHandler());
      			}
      		});
      		
      		ChannelFuture cf = b.connect("127.0.0.1", 8765).sync();
      		
      		cf.channel().writeAndFlush(Unpooled.copiedBuffer("hello netty!!".getBytes()));
      //		Thread.sleep(1000);
      //		cf.channel().writeAndFlush(Unpooled.copiedBuffer("hello netty!!".getBytes()));
      //		Thread.sleep(1000);
      //		cf.channel().writeAndFlush(Unpooled.copiedBuffer("hello netty!!".getBytes()));
      		//發(fā)送完畢,斷開連接
      		cf.addListener(ChannelFutureListener.CLOSE);
      		
      		cf.channel().closeFuture().sync();
      		worker.shutdownGracefully();
      		
      	}
      	
      

        

      ?

      clientHandler代碼:

      需要繼承:ChannelHandlerAdapter這個類

      public class ClientHandler extends ChannelHandlerAdapter {
      
      	@Override
      	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      		// TODO Auto-generated method stub
      		try {
      
                              //原始ByteBuf數(shù)據(jù)格式處理
      			ByteBuf buf = (ByteBuf)msg;
      			byte[] bytes = new byte[buf.readableBytes()];
      			buf.readBytes(bytes);
      			String result = new String(bytes, "utf-8");
      			System.out.println("Server: "   result);
      		}finally {
      
                             //接收處理完后,丟棄
      			ReferenceCountUtil.release(msg);
      		}
      		
      	}
      
      	@Override
      	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
      		// TODO Auto-generated method stub
      		cause.printStackTrace();
      		ctx.close();
      	}
      
      	
      }    
      

        

      ?

      Server代碼:

      public static void main(String[] args) throws InterruptedException {
      		
      		//第一個線程連接client端
      		EventLoopGroup boss = new NioEventLoopGroup();
      		//第二個線程處理邏輯
      		EventLoopGroup worker = new NioEventLoopGroup();
      		//輔助類,注冊 server
      		ServerBootstrap b = new ServerBootstrap();
      		b.group(boss, worker)
      		.channel(NioServerSocketChannel.class)
      		.childHandler(new ChannelInitializer<SocketChannel>() {
      
      			@Override
      			protected void initChannel(SocketChannel sc) throws Exception {
      				// TODO Auto-generated method stub
      				sc.pipeline().addLast(new ServerHandler());
      			}
      		});
      		
      		//綁定指定的端口方便監(jiān)聽
      		ChannelFuture cf = b.bind(8765).sync();
      		cf.channel().closeFuture().sync();
      		
      		boss.shutdownGracefully();
      		worker.shutdownGracefully();
      		
      	}
      

        

      ?

      serverHandler代碼:

      需要繼承:ChannelHandlerAdapter 類

      public class ServerHandler extends ChannelHandlerAdapter {
      
      	@Override
      	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      		// TODO Auto-generated method stub
      		ByteBuf buf = (ByteBuf)msg;
      		byte[] bs = new byte[buf.readableBytes()];
      		buf.readBytes(bs);
      		String result = new String(bs, "utf-8");
      		System.out.println("Client: "   result);
      		
      		String response = "888888";
      		ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
      		//.addListener(ChannelFutureListener.CLOSE);
      		
      	}
      
      	@Override
      	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
      		// TODO Auto-generated method stub
      		cause.printStackTrace();
      		ctx.close();
      	}
      
      	
      
      	
      }
      

        

      ?

      來源:http://www./content-4-214951.html

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多