Netty4 学习笔记之一:客户端与服务端通信 demo
来源:互联网 发布:高中网络课程哪个好 编辑:程序博客网 时间:2024/06/01 17:25
前言
因为以前在项目中使用过Mina框架,感受到了该框架的强大之处。于是在业余时间也学习了一下Netty。因为Netty的主要版本是Netty3和Netty4(Netty5已经被取消了),所以我就直接学习Netty4。在本文中演示的就是Netty的一个简单demo。
开发准备
Netty4的官方网站是:http://netty.io/ 。
本文使用的是Netty4.1。
由于Netty4.1版本需要JDK1.7,在使用JDK1.6时,会有一些bug,所以推荐使用JDK1.7。
因为Netty和Mina都是Trustin Lee的作品,所以在很多方面都十分相似,他们线程模型也是基本一致,采用了Reactors in threads模型,即Main Reactor + Sub Reactors的模式。
所以在开发上,很多都可以相互借鉴。
服务端
首先完成Netty服务端相关开发,和Mina一样。
主要由两个部分组成: 配置服务端的基本信息以及实现业务逻辑处理。
如果需要,还有filter过滤器。
服务端代码
import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;/** * * Title: NettyServer* Description: Netty服务端* Version:1.0.0 * @author Administrator* @date 2017-8-31 */public class NettyServer { private static final int port = 6789; //设置服务端端口 private static EventLoopGroup group = new NioEventLoopGroup(); // 通过nio方式来接收连接和处理连接 private static ServerBootstrap b = new ServerBootstrap(); /** * Netty创建全部都是实现自AbstractBootstrap。 * 客户端的是Bootstrap,服务端的则是 ServerBootstrap。 **/ public static void main(String[] args) throws InterruptedException { try { b.group(group); b.channel(NioServerSocketChannel.class); b.childHandler(new NettyServerFilter()); //设置过滤器 // 服务器绑定端口监听 ChannelFuture f = b.bind(port).sync(); System.out.println("服务端启动成功..."); // 监听服务器关闭监听 f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); ////关闭EventLoopGroup,释放掉所有资源包括创建的线程 } }}
服务端业务逻辑
import java.net.InetAddress;import java.util.Date;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;/** * * Title: HelloServerHandler* Description: 服务端业务逻辑* Version:1.0.0 * @author Administrator* @date 2017-8-31 */public class NettyServerHandler extends SimpleChannelInboundHandler<String> { /* * 收到消息时,返回信息 */ @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 收到消息直接打印输出 System.out.println("服务端接受的消息 : " + msg); if("quit".equals(msg)){//服务端断开的条件 ctx.close(); } Date date=new Date(); // 返回客户端消息 ctx.writeAndFlush(date+"\n"); } /* * 建立连接时,返回消息 */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("连接的客户端地址:" + ctx.channel().remoteAddress()); ctx.writeAndFlush("客户端"+ InetAddress.getLocalHost().getHostName() + "成功与服务端建立连接! \n"); super.channelActive(ctx); }}
服务端过滤器
import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.Delimiters;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;/** * * Title: HelloServerInitializer * Description: Netty 服务端过滤器 * Version:1.0.0 * @author Administrator * @date 2017-8-31 */public class NettyServerFilter extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline ph = ch.pipeline(); // 以("\n")为结尾分割的 解码器 ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); // 解码和编码,应和客户端一致 ph.addLast("decoder", new StringDecoder()); ph.addLast("encoder", new StringEncoder()); ph.addLast("handler", new NettyServerHandler());// 服务端业务逻辑 } }
客户端
客户端的主要工作是
1,连接到服务端
2,向服务端发送数据数据
3,处理服务端返回的数据
4,关闭连接
而且客户端相关代码也和服务端类似。
客户端
import io.netty.bootstrap.Bootstrap;import io.netty.channel.Channel;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioSocketChannel;import java.io.IOException;/** * * Title: NettyClient* Description: Netty客户端 * Version:1.0.0 * @author Administrator* @date 2017-8-31 */public class NettyClient { public static String host = "127.0.0.1"; //ip地址 public static int port = 6789; //端口 /// 通过nio方式来接收连接和处理连接 private static EventLoopGroup group = new NioEventLoopGroup(); private static Bootstrap b = new Bootstrap(); private static Channel ch; /** * Netty创建全部都是实现自AbstractBootstrap。 * 客户端的是Bootstrap,服务端的则是 ServerBootstrap。 **/ public static void main(String[] args) throws InterruptedException, IOException { System.out.println("客户端成功启动..."); b.group(group); b.channel(NioSocketChannel.class); b.handler(new NettyClientFilter()); // 连接服务端 ch = b.connect(host, port).sync().channel(); star(); } public static void star() throws IOException{ String str="Hello Netty"; ch.writeAndFlush(str+ "\r\n"); System.out.println("客户端发送数据:"+str); }}
客户端业务逻辑实现
import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;/** * * Title: NettyClientHandler* Description: 客户端业务逻辑实现* Version:1.0.0 * @author Administrator* @date 2017-8-31 */public class NettyClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println("客户端接受的消息: " + msg); } // @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("正在连接... "); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("连接关闭! "); super.channelInactive(ctx); }}
客户端过滤器
import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.DelimiterBasedFrameDecoder;import io.netty.handler.codec.Delimiters;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;/** * * Title: NettyClientFilter* Description: Netty客户端 过滤器* Version:1.0.0 * @author Administrator* @date 2017-8-31 */public class NettyClientFilter extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline ph = ch.pipeline(); /* * 解码和编码,应和服务端一致 * */ ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); ph.addLast("decoder", new StringDecoder()); ph.addLast("encoder", new StringEncoder()); ph.addLast("handler", new NettyClientHandler()); //客户端的逻辑 }}
效果实现图
服务端
客户端
demo教程到这里就结束了,如果有什么问题,欢迎提出!
阅读全文
4 0
- Netty4 学习笔记之一:客户端与服务端通信 demo
- Netty4 学习笔记之二:客户端与服务端心跳 demo
- NIO 客户端与服务端通信demo
- 警察与小偷的实现之一客户端与服务端通信
- ssl客户端与服务端通信的demo【转】
- 网络编程学习笔记(三)TCP协议及客户端与服务端交互Demo
- Android学习笔记之Socket客户端与服务端通信( 一)
- 服务端和客户端UDP通信demo
- 基于Delphi实现客户端服务端通信Demo
- socket通信 客户端与服务端
- 客户端与服务端通信原理
- Netty4.0学习笔记系列之一:Server与Client的通讯
- Netty4.0学习笔记系列之一:Server与Client的通讯
- Netty4.0学习笔记系列之一:Server与Client的通讯
- Netty4.0学习笔记系列之一:Server与Client的通讯
- Netty4.0学习笔记系列之一:Server与Client的通讯
- Netty4.0学习笔记系列之一:Server与Client的通讯
- Netty4.0学习笔记系列之一:Server与Client的通讯
- MySQL安装之后没有MySQL数据库的原因
- VirtualBox上Linux机器DHCP安装与配置
- Lucene6入门教程(六)搜索/查询方式
- Python 变量类型
- jQuery中$.ajax()的使用
- Netty4 学习笔记之一:客户端与服务端通信 demo
- Oracle数据库知识梳理---续(单行函数,多行函数,分组数据,分组过滤)
- 简易JAVA爬虫练习,为新手总结的三种爬虫方法
- offer大作战-JS实现排序算法
- java合成图片并添加文字
- TreeSet集合内部排序
- linux下使用openssl和md5sum加密文件或者字符串
- 超赞的word转pdf在线攻略,谁用谁知道
- AOP面向切面编程