<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 發送字符串代碼 ## service **EchoServer** ~~~ package com.netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class EchoServer { private final int port; public EchoServer(int port) { this.port = port; } public void start() throws InterruptedException { EventLoopGroup eventLoopGroup = null; try{ //server端引導類 ServerBootstrap serverBootstrap = new ServerBootstrap(); //連接池處理數據 eventLoopGroup = new NioEventLoopGroup(); //指定通道類型為NioServerSocketChannel,一種異步模式,OIO阻塞模式為OioServerSocketChannel // 設置InetSocketAddress讓服務器監聽某個端口已等待客戶端連接 // 設置childHandler執行所有的連接請求 serverBootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class) .localAddress("127.0.0.1",port) .childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel channel) throws Exception { //進 //注冊兩個InboundHandler,執行順序為注冊順序,所以應該是InboundHandler1 //InboundHandler2 //出 //注冊兩個OutboundHandler,執行順序為注冊順序的逆序,所以應該是OutboundHandler2 //OutboundHandler1 //在我業務處理中增加一系列的流水線,業務經過這些流水線就能得到結果了 channel.pipeline().addLast(new EchoInHandler1()); channel.pipeline().addLast(new EchoOutHandler1()); channel.pipeline().addLast(new EchoOutHandler2()); channel.pipeline().addLast(new EchoInHandler2()); } }); //最后綁定服務器等待直到綁定完成,調用sync()方法會阻塞直到服務器完成綁定 ChannelFuture channelFuture = serverBootstrap.bind().sync(); System.out.println("開始監聽,端口為: "+channelFuture.channel().localAddress()); //等待channel關閉,因為使用sync(),所以關閉操作也會被阻塞,調用sync()方法會阻塞直到服務器關閉 channelFuture.channel().closeFuture().sync(); }catch (Exception e){ e.printStackTrace(); }finally { // 阻塞等待線程組關閉 eventLoopGroup.shutdownGracefully().sync(); } } public static void main(String[] args) throws InterruptedException { new EchoServer(20000).start(); } } ~~~ **EchoInHandler1** ~~~ package com.netty; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class EchoInHandler1 extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("in1"); // 用fireChannelRead發送到下一個InboundHandler ctx.fireChannelRead(msg); //這個方法走完會走channelReadComplete } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); //刷新后才將數據發出到SocketChannel } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //如果發生異常走這個 cause.printStackTrace(); //把連接關閉 ctx.close(); } } ~~~ **EchoInHandler2** ~~~ package com.netty; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.Date; public class EchoInHandler2 extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("in2"); ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); //解碼 String body = new String(req, "UTF-8"); System.out.println("接收客戶端數據:" + body); //向客戶端寫數據 System.out.println("server向client發送數據"); String currentTime = new Date(System.currentTimeMillis()).toString(); //把數據變成ByteBuf ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes()); //開始準備發,然后會走OutHandler ctx.write(resp); //方法執行完會走channelReadComplete } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { //刷新后才將數據發出到SocketChannel ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //異常處理 cause.printStackTrace(); ctx.close(); } } ~~~ **EchoOutHandler1** ~~~ package com.netty; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; public class EchoOutHandler1 extends ChannelOutboundHandlerAdapter { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { System.out.println("out1"); //msg就是InHandler發過來的,你可以對他再次加工 System.out.println(msg); ctx.write(msg); ctx.flush(); } } ~~~ **EchoOutHandler2** ~~~ package com.netty; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; public class EchoOutHandler2 extends ChannelOutboundHandlerAdapter { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { System.out.println("out2"); ctx.write(msg); // super.write(ctx, msg, promise); } } ~~~ ## client **EchoClient** ~~~ package com.nettyClient; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; public class EchoClient { private final String host; private final int port; public EchoClient(String host, int port) { this.host = host; this.port = port; } public void start() throws Exception { EventLoopGroup nioEventLoopGroup = null; try { // 客戶端引導類 Bootstrap bootstrap = new Bootstrap(); // EventLoopGroup可以理解為是一個線程池,這個線程池用來處理連接、接受數據、發送數據 nioEventLoopGroup = new NioEventLoopGroup(); bootstrap.group(nioEventLoopGroup)//多線程處理 .channel(NioSocketChannel.class)//指定通道類型為NioServerSocketChannel,一種異步模式,OIO阻塞模式為OioServerSocketChannel .remoteAddress(new InetSocketAddress(host, port))//地址 .handler(new ChannelInitializer<SocketChannel>() {//業務處理類 @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoClientHandler());//注冊handler } }); // 連接服務器 ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { nioEventLoopGroup.shutdownGracefully().sync(); } } public static void main(String[] args) throws Exception { new EchoClient("127.0.0.1", 20000).start(); } } ~~~ **EchoClientHandler** ~~~ package com.nettyClient; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { // 客戶端連接服務器后被調用 @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("客戶端連接服務器,開始發送數據……"); byte[] req = "QUERY TIME ORDER".getBytes();//消息 ByteBuf firstMessage = Unpooled.buffer(req.length);//創建一個空的ByteBuff用于緩存即將發送的數據 firstMessage.writeBytes(req);//發送 ctx.writeAndFlush(firstMessage);//flush } // ? 從服務器接收到數據后調用 @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { System.out.println("client 讀取server數據.."); // 服務端返回消息后 ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8"); System.out.println("服務端返回的數據為 :" + body); } // ? 發生異常時被調用 @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println("client exceptionCaught.."); // 釋放資源 ctx.close(); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看