Server4.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import io.netty.bootstrap.ServerBootstrap;
  2. import io.netty.channel.*;
  3. import io.netty.channel.nio.NioEventLoopGroup;
  4. import io.netty.channel.socket.SocketChannel;
  5. import io.netty.channel.socket.nio.NioServerSocketChannel;
  6. import io.netty.handler.codec.string.StringDecoder;
  7. import io.netty.handler.codec.string.StringEncoder;
  8. /**
  9. * 测试客户端
  10. *
  11. * @author 修唯xiuwei
  12. * @create 2018-02-05 15:56
  13. * @Email 524710549@qq.com
  14. **/
  15. public class Server4 {
  16. public static void main(String[] args) {
  17. EventLoopGroup boss = new NioEventLoopGroup();
  18. EventLoopGroup worker = new NioEventLoopGroup();
  19. try {
  20. //辅助启动类
  21. ServerBootstrap bootstrap = new ServerBootstrap();
  22. //设置线程池
  23. bootstrap.group(boss, worker);
  24. //设置socket工厂
  25. bootstrap.channel(NioServerSocketChannel.class);
  26. //设置管道工厂
  27. bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
  28. @Override
  29. protected void initChannel(SocketChannel socketChannel) throws Exception {
  30. //获取管道
  31. ChannelPipeline pipeline = socketChannel.pipeline();
  32. //字符串解码器
  33. pipeline.addLast(new StringDecoder());
  34. //字符串编码器
  35. pipeline.addLast(new StringEncoder());
  36. //处理类
  37. pipeline.addLast(new ServerHandler4());
  38. }
  39. });
  40. //设置TCP参数
  41. //1.链接缓冲池的大小(ServerSocketChannel的设置)
  42. bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
  43. //维持链接的活跃,清除死链接(SocketChannel的设置)
  44. bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
  45. //关闭延迟发送
  46. bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
  47. //绑定端口
  48. ChannelFuture future = bootstrap.bind(9001).sync();
  49. System.out.println("server start ...... ");
  50. //等待服务端监听端口关闭
  51. future.channel().closeFuture().sync();
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. } finally {
  55. //优雅退出,释放线程池资源
  56. boss.shutdownGracefully();
  57. worker.shutdownGracefully();
  58. }
  59. }
  60. }
  61. class ServerHandler4 extends SimpleChannelInboundHandler<String> {
  62. //读取客户端发送的数据
  63. @Override
  64. protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
  65. System.out.println("client response :" + msg);
  66. }
  67. //新客户端接入
  68. @Override
  69. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  70. System.out.println("channelActive");
  71. }
  72. //客户端断开
  73. @Override
  74. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  75. System.out.println("channelInactive");
  76. }
  77. //异常
  78. @Override
  79. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  80. //关闭通道
  81. ctx.channel().close();
  82. //打印异常
  83. cause.printStackTrace();
  84. }
  85. }