zoukankan      html  css  js  c++  java
  • netty 实现socket服务端编写

     1 import java.net.InetSocketAddress;
     2 
     3 import io.netty.bootstrap.ServerBootstrap;
     4 import io.netty.channel.ChannelFuture;
     5 import io.netty.channel.ChannelInitializer;
     6 import io.netty.channel.ChannelOption;
     7 import io.netty.channel.EventLoopGroup;
     8 import io.netty.channel.nio.NioEventLoopGroup;
     9 import io.netty.channel.socket.SocketChannel;
    10 import io.netty.channel.socket.nio.NioServerSocketChannel;
    11 
    12 public class TimeServer {
    13     
    14     
    15     public static void main(String[] args) throws Exception{
    16         new TimeServer().bind("192.168.1.102", 8400);
    17     }
    18     
    19 
    20     public void bind(String addr,int port) {
    21         //配置服务端的nio线程组
    22         EventLoopGroup boosGroup=new NioEventLoopGroup();
    23         EventLoopGroup workerGroup=new NioEventLoopGroup();
    24         try {
    25             ServerBootstrap b=new ServerBootstrap();
    26             b.group(boosGroup,workerGroup);
    27             b.channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,1024)
    28             .childHandler(new ChildChannelHandler());
    29             //绑定端口,同步等待成功
    30             ChannelFuture f=b.bind(new InetSocketAddress(addr, port)).sync();
    31             //等等服务器端监听端口关闭
    32             f.channel().closeFuture().sync();
    33         } catch (Exception e) {
    34             // TODO: handle exception
    35         }finally{
    36             boosGroup.shutdownGracefully();
    37             workerGroup.shutdownGracefully();
    38         }
    39     }
    40     
    41     private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
    42 
    43         @Override
    44         protected void initChannel(SocketChannel ch) throws Exception {
    45             ch.pipeline().addLast(new TimeServerHandler());
    46             
    47         }
    48         
    49     }
    50     
    51     
    52 }
     1 import java.nio.ByteBuffer;
     2 import java.text.SimpleDateFormat;
     3 import java.util.Date;
     4 
     5 import io.netty.buffer.ByteBuf;
     6 import io.netty.buffer.Unpooled;
     7 import io.netty.channel.ChannelHandlerAdapter;
     8 import io.netty.channel.ChannelHandlerContext;
     9 
    10 public class TimeServerHandler extends ChannelHandlerAdapter {
    11 
    12     @Override
    13     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    14         ByteBuf buf=(ByteBuf)msg;
    15         byte[] bytes=new byte[buf.readableBytes()];
    16         buf.readBytes(bytes);
    17         String body=new String(bytes,"UTF-8");
    18         SimpleDateFormat  dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    19         String time=dateFormat.format(new Date());
    20         String res="来自与服务端的回应,时间:"+ time;
    21         ByteBuf resp=Unpooled.copiedBuffer(res.getBytes());
    22         ctx.write(resp);
    23         
    24     }
    25 
    26     @Override
    27     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    28         ctx.flush();
    29     }
    30 
    31     @Override
    32     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    33         ctx.close();
    34     }
    35 
    36     
    37 
    38     
    39 }

    这个比传统的nio好用多了,netty的版本为netty-all-5.0.0.Alpha1.jar

    天天学习,天天进步

  • 相关阅读:
    String和StringBuilder和StringBuffer
    多态
    组件
    反向代理
    基础知识
    reflection
    v-model 与 v-bind:model
    tomcat端口占用问题
    socket
    简要概括内存机制
  • 原文地址:https://www.cnblogs.com/huzi007/p/5506586.html
Copyright © 2011-2022 走看看