NettyTheSniper

导航

基于Netty5.0入门案例二之NettyServer接收数据

前言介绍:

    凡是新知识都需要有个入门的案例,一个简单的输入输出就能解除你当前遇到的所有疑惑。不要总想着先学理论后学实战,新东方还135学理论,246学实战呢【800个床位不锈钢】。

    欢迎加入:itstack | Netty The Sniper 5360692

环境需求:

1、jdk1.7以上【jdk1.7以下只能部分支持netty】

2、Netty-all-5.0【netty3.x 4.x 5每次的变化较大,接口类名也随着变化】

3、telnet 测试【可以现在你的win7机器上测试这个命令,用于链接到服务端的测试命令】

4、建议下载个网络调试助手,它能帮助你测试服务端、客户端



代码部分:

======================

TestNettyServerBaseDemo

    src

        com.itstack

            ChildChannelHandler.java

            MyServerHanlder.java

            NettyServer.java

======================


ChildChannelHandler.java


  1. package com.itstack;
  2.  
  3. import io.netty.channel.ChannelInitializer;
  4. import io.netty.channel.socket.SocketChannel;
  5.  
  6. public class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
  7.  
  8. @Override
  9. protected void initChannel(SocketChannel e) throws Exception {
  10.  
  11. System.out.println("报告");
  12. System.out.println("信息:有一客户端链接到本服务端");
  13. System.out.println("IP:"+e.localAddress().getHostName());
  14. System.out.println("Port:"+e.localAddress().getPort());
  15. System.out.println("报告完毕");
  16. //在管道中添加我们自己的接收数据实现方法
  17. e.pipeline().addLast(new MyServerHanlder());
  18. }
  19.  
  20. }



MyServerHanlder.java


  1. package com.itstack;
  2.  
  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. import io.netty.handler.codec.bytes.ByteArrayDecoder;
  10.  
  11. public class MyServerHanlder extends ChannelHandlerAdapter{
  12.  
  13. /*
  14. * channelAction
  15. *
  16. * channel 通道
  17. * action 活跃的
  18. *
  19. * 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据
  20. *
  21. */
  22. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  23. System.out.println(ctx.channel().localAddress().toString()+" channelActive");
  24. }
  25. /*
  26. * channelInactive
  27. *
  28. * channel 通道
  29. * Inactive 不活跃的
  30. *
  31. * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
  32. *
  33. */
  34. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  35. System.out.println(ctx.channel().localAddress().toString()+" channelInactive");
  36. }
  37. /*
  38. * channelRead
  39. *
  40. * channel 通道
  41. * Read 读
  42. *
  43. * 简而言之就是从通道中读取数据,也就是服务端接收客户端发来的数据
  44. * 但是这个数据在不进行解码时它是ByteBuf类型的后面例子我们在介绍
  45. *
  46. */
  47. public void channelRead(ChannelHandlerContext ctx, Object msg)
  48. throws Exception {
  49. ByteBuf buf = (ByteBuf) msg;
  50. byte[] msgByte = new byte[buf.readableBytes()];
  51. buf.readBytes(msgByte);
  52. System.out.println(new Date()+" "+new String(msgByte,"UTF-8"));
  53. }
  54. /*
  55. * channelReadComplete
  56. *
  57. * channel 通道
  58. * Read 读取
  59. * Complete 完成
  60. *
  61. * 在通道读取完成后会在这个方法里通知,对应可以做刷新操作
  62. * ctx.flush()
  63. *
  64. */
  65. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  66. ctx.flush();
  67. }
  68. /*
  69. * exceptionCaught
  70. *
  71. * exception 异常
  72. * Caught 抓住
  73. *
  74. * 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接
  75. *
  76. */
  77. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
  78. throws Exception {
  79. ctx.close();
  80. System.out.println("异常信息:\r\n"+cause.getMessage());
  81. }
  82. }



NettyServer.java

 

  1. package com.itstack;
  2.  
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.channel.ChannelFuture;
  5. import io.netty.channel.ChannelOption;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.nio.NioEventLoopGroup;
  8. import io.netty.channel.socket.nio.NioServerSocketChannel;
  9.  
  10. public class NettyServer {
  11.  
  12. public static void main(String[] args) {
  13. try {
  14. System.out.println("服务端开启等待客户端链接");
  15. new NettyServer().bing(7397);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. public void bing(int port) throws Exception{
  21. EventLoopGroup bossGroup = new NioEventLoopGroup();
  22. EventLoopGroup workGroup = new NioEventLoopGroup();
  23. try {
  24. ServerBootstrap b = new ServerBootstrap();
  25. b.group(bossGroup, workGroup);
  26. b.channel(NioServerSocketChannel.class);
  27. b.option(ChannelOption.SO_BACKLOG, 1024);
  28. b.childHandler(new ChildChannelHandler());
  29. // 绑定端口
  30. ChannelFuture f = b.bind(port).sync();
  31. // 等待服务端监听端口关闭
  32. f.channel().closeFuture().sync();
  33. } finally {
  34. // 优雅的退出
  35. bossGroup.shutdownGracefully();
  36. workGroup.shutdownGracefully();
  37. }
  38. }
  39. }

测试运行:

1、启动NettyServer

2、控制台输出:

----------------------------------------------

服务端开启等待客户端链接

----------------------------------------------

3、开启DOS

4、输入telnet localhost 7397

5、控制台输出:

----------------------------------------------

报告
信息:有一客户端链接到本服务端
IP:localhost.localdomain
Port:7397
报告完毕
localhost.localdomain/127.0.0.1:7397 channelActive
Tue Dec 30 13:39:53 CST 2014 1
Tue Dec 30 13:39:54 CST 2014 1
Tue Dec 30 13:39:55 CST 2014 1
Tue Dec 30 13:39:55 CST 2014 2
Tue Dec 30 13:39:55 CST 2014 2
Tue Dec 30 13:39:55 CST 2014 2
Tue Dec 30 13:39:56 CST 2014 3
Tue Dec 30 13:39:56 CST 2014 3
Tue Dec 30 13:39:56 CST 2014 3
localhost.localdomain/127.0.0.1:7397 channelInactive

----------------------------------------------

posted on 2015-01-12 16:59  bugstack虫洞栈  阅读(439)  评论(0)    收藏  举报