http://blog.csdn.net/u013252773/article/details/21046697

本文是学习Netty的第一篇文章,主要对Netty的Server和Client间的通讯机制进行验证。

Server与Client建立连接后,会执行以下的步骤:

1、Client向Server发送消息:Are you ok?

2、Server接收客户端发送的消息,并打印出来。

3、Server端向客户端发送消息:I am ok!

4、Client接收Server端发送的消息,并打印出来,通讯结束。

涉及到的类有4个:

1、HelloServer :server类,启动Netty server

2、HelloServerInHandler:server的handler,接收客户端消息,并向客户端发送消息

3、HelloClient:client类,建立于Netty server的连接

4、HelloClientIntHandler:client的handler,接收server端的消息,并向服务端发送消息

1、HelloServer代码如下:

  1. package com.guowl.testserver;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.channel.ChannelFuture;
  4. import io.netty.channel.ChannelInitializer;
  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.SocketChannel;
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;
  10. public class HelloServer {
  11. public void start(int port) throws Exception {
  12. EventLoopGroup bossGroup = new NioEventLoopGroup();
  13. EventLoopGroup workerGroup = new NioEventLoopGroup();
  14. try {
  15. ServerBootstrap b = new ServerBootstrap();
  16. b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
  17. .childHandler(new ChannelInitializer<SocketChannel>() {
  18. @Override
  19. public void initChannel(SocketChannel ch)
  20. throws Exception {
  21. // 注册handler
  22. ch.pipeline().addLast(new HelloServerInHandler());
  23. }
  24. }).option(ChannelOption.SO_BACKLOG, 128)
  25. .childOption(ChannelOption.SO_KEEPALIVE, true);
  26. ChannelFuture f = b.bind(port).sync();
  27. f.channel().closeFuture().sync();
  28. } finally {
  29. workerGroup.shutdownGracefully();
  30. bossGroup.shutdownGracefully();
  31. }
  32. }
  33. public static void main(String[] args) throws Exception {
  34. HelloServer server = new HelloServer();
  35. server.start(8000);
  36. }
  37. }

2、HelloServerInHandler代码如下:

  1. package com.guowl.testserver;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.channel.ChannelHandlerContext;
  4. import io.netty.channel.ChannelInboundHandlerAdapter;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. // 该handler是InboundHandler类型
  8. public class HelloServerInHandler extends ChannelInboundHandlerAdapter {
  9. private static Logger logger = LoggerFactory
  10. .getLogger(HelloServerInHandler.class);
  11. @Override
  12. public void channelRead(ChannelHandlerContext ctx, Object msg)
  13. throws Exception {
  14. logger.info("HelloServerInHandler.channelRead");
  15. ByteBuf result = (ByteBuf) msg;
  16. byte[] result1 = new byte[result.readableBytes()];
  17. // msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中
  18. result.readBytes(result1);
  19. String resultStr = new String(result1);
  20. // 接收并打印客户端的信息
  21. System.out.println("Client said:" + resultStr);
  22. // 释放资源,这行很关键
  23. result.release();
  24. // 向客户端发送消息
  25. String response = "I am ok!";
  26. // 在当前场景下,发送的数据必须转换成ByteBuf数组
  27. ByteBuf encoded = ctx.alloc().buffer(4 * response.length());
  28. encoded.writeBytes(response.getBytes());
  29. ctx.write(encoded);
  30. ctx.flush();
  31. }
  32. @Override
  33. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  34. ctx.flush();
  35. }
  36. }

3、HelloClient代码如下:

  1. package com.guowl.testserver;
  2. import io.netty.bootstrap.Bootstrap;
  3. import io.netty.channel.ChannelFuture;
  4. import io.netty.channel.ChannelInitializer;
  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.SocketChannel;
  9. import io.netty.channel.socket.nio.NioSocketChannel;
  10. public class HelloClient {
  11. public void connect(String host, int port) throws Exception {
  12. EventLoopGroup workerGroup = new NioEventLoopGroup();
  13. try {
  14. Bootstrap b = new Bootstrap();
  15. b.group(workerGroup);
  16. b.channel(NioSocketChannel.class);
  17. b.option(ChannelOption.SO_KEEPALIVE, true);
  18. b.handler(new ChannelInitializer<SocketChannel>() {
  19. @Override
  20. public void initChannel(SocketChannel ch) throws Exception {
  21. ch.pipeline().addLast(new HelloClientIntHandler());
  22. }
  23. });
  24. // Start the client.
  25. ChannelFuture f = b.connect(host, port).sync();
  26. // Wait until the connection is closed.
  27. f.channel().closeFuture().sync();
  28. } finally {
  29. workerGroup.shutdownGracefully();
  30. }
  31. }
  32. public static void main(String[] args) throws Exception {
  33. HelloClient client = new HelloClient();
  34. client.connect("127.0.0.1", 8000);
  35. }
  36. }

4、HelloClientIntHandler代码如下:

  1. package com.guowl.testserver;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.channel.ChannelHandlerContext;
  4. import io.netty.channel.ChannelInboundHandlerAdapter;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. public class HelloClientIntHandler extends ChannelInboundHandlerAdapter {
  8. private static Logger logger = LoggerFactory.getLogger(HelloClientIntHandler.class);
  9. // 接收server端的消息,并打印出来
  10. @Override
  11. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  12. logger.info("HelloClientIntHandler.channelRead");
  13. ByteBuf result = (ByteBuf) msg;
  14. byte[] result1 = new byte[result.readableBytes()];
  15. result.readBytes(result1);
  16. System.out.println("Server said:" + new String(result1));
  17. result.release();
  18. }
  19. // 连接成功后,向server发送消息
  20. @Override
  21. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  22. logger.info("HelloClientIntHandler.channelActive");
  23. String msg = "Are you ok?";
  24. ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());
  25. encoded.writeBytes(msg.getBytes());
  26. ctx.write(encoded);
  27. ctx.flush();
  28. }
  29. }

通过上面简单的实例可以发现:

1、在没有任何encoder、decoder的情况下,Netty发送接收数据都是按照ByteBuf的形式,其它形式都是不合法的。

2、接收发送数据操作都是通过handler实现的,handler在netty中占据了非常重要的位置。

3、netty的handler是基于事件触发的,例如当client连接server成功后,client中的HelloClientIntHandler的channelActive方法会自动调用。

Netty4.0学习笔记系列之一:Server与Client的通讯的更多相关文章

  1. Netty4.0学习笔记系列之四:混合使用coder和handler

    Handler如何使用在前面的例子中已经有了示范,那么同样是扩展自ChannelHandler的Encoder和Decoder,与Handler混合后又是如何使用的?本文将通过一个实际的小例子来展示它 ...

  2. Netty4.0学习笔记系列之二:Handler的执行顺序(转)

    http://blog.csdn.net/u013252773/article/details/21195593 Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet ...

  3. Netty4.0学习笔记系列之三:构建简单的http服务(转)

    http://blog.csdn.net/u013252773/article/details/21254257 本文主要介绍如何通过Netty构建一个简单的http服务. 想要实现的目的是: 1.C ...

  4. Netty4.0学习笔记系列之二:Handler的执行顺序

    Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通过Handler可以完成通讯报文的解码编码.拦截指定的报文.统一对日志错误进行处理.统一对 ...

  5. Apache Ignite 学习笔记(三): Ignite Server和Client节点介绍

    在前两篇文章中,我们把Ignite集群当做一个黑盒子,用二进制包自带的脚本启动Ignite节点后,我们用不同的客户端连接上Ignite进行操作,展示了Ignite作为一个分布式内存缓存,内存数据库的基 ...

  6. SQLServer学习笔记系列2

    一.写在前面的话 继上一次SQLServer学习笔记系列1http://www.cnblogs.com/liupeng61624/p/4354983.html以后,继续学习Sqlserver,一步一步 ...

  7. 步步为营 SharePoint 开发学习笔记系列总结

    转:http://www.cnblogs.com/springyangwc/archive/2011/08/03/2126763.html 概要 为时20多天的sharepoint开发学习笔记系列终于 ...

  8. Redis 学习笔记系列文章之 Redis 的安装与配置 (一)

    1. 介绍 Redis is an open source (BSD licensed), in-memory data structure store, used as database, cach ...

  9. WebService学习笔记系列(二)

    soap(简单对象访问协议),它是在http基础之上传递xml格式数据的协议.soap协议分为两个版本,soap1.1和soap1.2. 在学习webservice时我们有一个必备工具叫做tcpmon ...

随机推荐

  1. ||和 && 符号的赋值运用(转)

    javascript “||”.“&&”的灵活运用 博客分类: Jquery javascript javascript中运用“||”.“&&”javascript 真 ...

  2. Qt自定义控件(插件)并添加到QtDesigher

    之前使用Qt的时候都是手写代码的(因为批量按钮可以使用数组实现),但当界面越来越复杂时,这种开发效率就太低了; 后来就开始使用QtDesigner,但要使QtDesigner支持我自己写的控件,需要提 ...

  3. poj1066

    很好的一道题.题意是,一个正方形围墙内有一些交错的内墙,内墙的端点都在正方形上,在正方形内部有一个点,求从正方形外到这个点的最少要走的门数,门只能是线段的中点. 思路很巧妙,因为从一个点到终点不可能“ ...

  4. MyEclipse8.6安装svn(非link方式)

    此方法经试验可行,暂时可以作为最佳解决方案. 本文所使用的MyEclipse版本为:8.6.1 svn的eclipse插件版本为:1.6.17,下载地址:http://subclipse.tigris ...

  5. 搬移到GitHub Page啦~

    GitHub: https://github.com/BOT-Man-JL/ Page: https://BOT-Man-JL.github.io/

  6. 自定义 textField 的清除 button

    UIButton *clearButton = [self.textField valueForKey:@"_clearButton"]; [clearButton setImag ...

  7. 调优系列-tomcat调优

    http://www.360doc.com/content/14/1208/13/16070877_431273418.shtml 使用JMeter对Tomcat进行压力测试与Tomcat性能调优 n ...

  8. idea+maven

    使用IntelliJ IDEA开发SpringMVC网站(一)开发环境 http://my.oschina.net/gaussik/blog/385697 使用IntelliJ IDEA开发Sprin ...

  9. bootstrap 下的 validation插件

    http://reactiveraven.github.io/jqBootstrapValidation/

  10. iOS 小知识 - #if , #ifdef , #ifndef.

    Q : 在项目的 .h 文件中, #ifndef XXX_h #define XXX_h //程序段1 #endif  /* XXX_h */ 的作用? A : 如果 XXX.h 不存在,就引入 XX ...