NettyTheSniper

导航

基于Netty5.0入门案例四之NettyServer收发数据

前言介绍:

    本案例主要介绍如何在服务端接收到数据后发送数据,当然netty是基于NIO的,所以收发数据不互相阻塞。

    本案例技术点:

    1、ByteBuf

    2、Unpooled

    3、writeAndFlush

   欢迎加入: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. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.Unpooled;
  4. import io.netty.channel.ChannelInitializer;
  5. import io.netty.channel.socket.SocketChannel;
  6. import io.netty.handler.codec.DelimiterBasedFrameDecoder;
  7. import io.netty.handler.codec.Delimiters;
  8. import io.netty.handler.codec.FixedLengthFrameDecoder;
  9. import io.netty.handler.codec.LineBasedFrameDecoder;
  10. import io.netty.handler.codec.string.StringDecoder;
  11. import io.netty.handler.codec.string.StringEncoder;
  12. public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
  13. @Override
  14. protected void initChannel(SocketChannel e) throws Exception {
  15. System.out.println("报告");
  16. System.out.println("信息:有一客户端链接到本服务端");
  17. System.out.println("IP:" + e.localAddress().getHostName());
  18. System.out.println("Port:" + e.localAddress().getPort());
  19. System.out.println("报告完毕");
  20. // 解码器
  21. // 基于换行符号
  22. e.pipeline().addLast(new LineBasedFrameDecoder(1024));
  23. // 基于指定字符串【换行符,这样功能等同于LineBasedFrameDecoder】
  24. // e.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, false, Delimiters.lineDelimiter()));
  25. // 基于最大长度
  26. // e.pipeline().addLast(new FixedLengthFrameDecoder(4));
  27. // 解码转Sring
  28. e.pipeline().addLast(new StringDecoder());
  29. // 在管道中添加我们自己的接收数据实现方法
  30. e.pipeline().addLast(new MyServerHanlder());
  31. }
  32. }

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. String str = "您已经开启与服务端链接"+" "+new Date()+" "+ctx.channel().localAddress();
  26. ByteBuf buf = Unpooled.buffer(str.getBytes().length);
  27. buf.writeBytes(str.getBytes());
  28. ctx.writeAndFlush(buf);
  29. }
  30. /*
  31. * channelInactive
  32. *
  33. * channel 通道
  34. * Inactive 不活跃的
  35. *
  36. * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
  37. *
  38. */
  39. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  40. System.out.println(ctx.channel().localAddress().toString()+" channelInactive");
  41. }
  42. /*
  43. * channelRead
  44. *
  45. * channel 通道
  46. * Read 读
  47. *
  48. * 简而言之就是从通道中读取数据,也就是服务端接收客户端发来的数据
  49. * 但是这个数据在不进行解码时它是ByteBuf类型的后面例子我们在介绍
  50. *
  51. */
  52. public void channelRead(ChannelHandlerContext ctx, Object msg)
  53. throws Exception {
  54. //注意此处已经不需要手工解码了
  55. System.out.println(new Date()+" "+msg);
  56. //通知您已经链接上客户端
  57. String str = "服务端收到:"+new Date()+" "+msg;
  58. ByteBuf buf = Unpooled.buffer(str.getBytes().length);
  59. buf.writeBytes(str.getBytes());
  60. ctx.writeAndFlush(buf);
  61. }
  62. /*
  63. * channelReadComplete
  64. *
  65. * channel 通道
  66. * Read 读取
  67. * Complete 完成
  68. *
  69. * 在通道读取完成后会在这个方法里通知,对应可以做刷新操作
  70. * ctx.flush()
  71. *
  72. */
  73. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  74. ctx.flush();
  75. }
  76. /*
  77. * exceptionCaught
  78. *
  79. * exception 异常
  80. * Caught 抓住
  81. *
  82. * 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接
  83. *
  84. */
  85. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
  86. throws Exception {
  87. ctx.close();
  88. System.out.println("异常信息:\r\n"+cause.getMessage());
  89. }
  90. }


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、开启网络调试助手


4、根据测试点输入不同的数据类型【换行的、特殊字符的、不同长度的】


5、

客户端控制台输出:

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

您已经开启与服务端链接 Tue Dec 30 15:55:40 CST 2014 user-PC/192.168.30.223:7397
服务端收到:Tue Dec 30 15:55:44 CST 2014 群号:5360692
服务端收到:Tue Dec 30 15:55:45 CST 2014 群号:5360692
服务端收到:Tue Dec 30 15:55:45 CST 2014 群号:5360692
服务端收到:Tue Dec 30 15:55:46 CST 2014 群号:5360692
服务端收到:Tue Dec 30 15:55:46 CST 2014 群号:5360692

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

服务端端控制台输出:

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

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

报告
信息:有一客户端链接到本服务端
IP:user-PC
Port:7397
报告完毕
user-PC/192.168.30.223:7397 channelActive
Tue Dec 30 15:55:44 CST 2014 群号:5360692
Tue Dec 30 15:55:45 CST 2014 群号:5360692
Tue Dec 30 15:55:45 CST 2014 群号:5360692
Tue Dec 30 15:55:46 CST 2014 群号:5360692
Tue Dec 30 15:55:46 CST 2014 群号:5360692
user-PC/192.168.30.223:7397 channelInactive

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

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