Netty学习笔记一 项目demo
来源:互联网 发布:mastercam9.1编程视频 编辑:程序博客网 时间:2024/05/22 12:23
Netty是一个由JBoss提供的异步的,事件驱动的网络应用程序框架工具,用来开发高性能 高可靠性的网络服务器和客户端程序。
Netty 是一个基于NIO的客户/服务端编程框架,可以简化网络应用的编程开发过程。
这是官网上的结构图:
在线文档地址:http://netty.io/5.0/api/index.html
中文guide:http://ifeve.com/netty5-user-guide
一、java下Netty学习
1.下载 http://netty.io/ 据说5.0版本被废弃了,现在我下载的是4.1.6
2.代码学习
二、Discard丢弃服务:
1.新建 java项目,把下载的文件解压,把jar/all-in-one放到项目里,并加到BuildPath
2.把官方测试代码运行起来:
package io.netty.example.discard;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelHandlerAdapter;/** * Handles a server-side channel. */public class DiscardServerHandler extends ChannelHandlerAdapter { // (1) @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2) // Discard the received data silently. ((ByteBuf) msg).release(); // (3) } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4) // Close the connection when an exception is raised. cause.printStackTrace(); ctx.close(); }}
package io.netty.example.discard;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;/** * Discards any incoming data. */public class DiscardServer {private int port;public DiscardServer(int port) {this.port = port;}public void run() throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap(); // (2)b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3).childHandler(new ChannelInitializer<SocketChannel>() { // (4)@Overridepublic void initChannel(SocketChannel ch)throws Exception {ch.pipeline().addLast(new DiscardServerHandler());}}).option(ChannelOption.SO_BACKLOG, 128) // (5).childOption(ChannelOption.SO_KEEPALIVE, true); // (6)// Bind and start to accept incoming connections.ChannelFuture f = b.bind(port).sync(); // (7)// Wait until the server socket is closed.// In this example, this does not happen, but you can do that to// gracefully// shut down your server.f.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {int port;if (args.length > 0) {port = Integer.parseInt(args[0]);} else {port = 8080;}new DiscardServer(port).run();}}
设下断点,debug运行:
启动PuTTY,连接本机8080端口,Connection type:选择Telnet,
Telnet连接时,和发送字符时,程序会停在16行位置。
修改channelRead函数:
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2) // Discard the received data silently. ctx.write(msg); ctx.flush(); }
再次运行,现在telnet发送数据后,会回显出来。
TIME服务
添加类:
package io.netty.example.time;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;public class TimeServerHandler extends ChannelHandlerAdapter { @Override public void channelActive(final ChannelHandlerContext ctx) { // (1) final ByteBuf time = ctx.alloc().buffer(4); // (2) time.writeInt((int) (System.currentTimeMillis() / 1000L + 2208988800L)); final ChannelFuture f = ctx.writeAndFlush(time); // (3) f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { assert f == future; ctx.close(); } }); // (4) } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); }}
修改DiscardServer
package io.netty.example.discard;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.example.time.TimeServerHandler;/** * Discards any incoming data. */public class DiscardServer {private int port;public DiscardServer(int port) {this.port = port;}public void run() throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap(); // (2)b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3).childHandler(new ChannelInitializer<SocketChannel>() { // (4)@Overridepublic void initChannel(SocketChannel ch)throws Exception {ch.pipeline().addLast(new TimeServerHandler());}}).option(ChannelOption.SO_BACKLOG, 128) // (5).childOption(ChannelOption.SO_KEEPALIVE, true); // (6)// Bind and start to accept incoming connections.ChannelFuture f = b.bind(port).sync(); // (7)// Wait until the server socket is closed.// In this example, this does not happen, but you can do that to// gracefully// shut down your server.f.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {int port;if (args.length > 0) {port = Integer.parseInt(args[0]);} else {port = 8080;}new DiscardServer(port).run();}}
在Ubuntu下安装rdate后测试:
Time客户端
另建一个项目:
package io.netty.example.time;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;public class TimeClient { public static void main(String[] args) throws Exception { String host = args[0]; int port = Integer.parseInt(args[1]); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); // (1) b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // (5) // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }}
package io.netty.example.time;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import java.util.Date;public class TimeClientHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf m = (ByteBuf) msg; // (1) try { long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L; System.out.println(new Date(currentTimeMillis)); ctx.close(); } finally { m.release(); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); }}
运行结果:
1 0
- Netty学习笔记一 项目demo
- 【Netty】netty学习笔记一
- Netty学习笔记一
- Netty 学习笔记一、Hello Netty
- Netty学习笔记(一) 配置Netty环境
- Netty学习笔记(一)
- netty学习笔记(一)
- Netty学习笔记(一)
- Java Netty 学习笔记(一)初识Netty
- Netty学习笔记(一)EchoServer服务器
- netty学习一:用netty构造http服务的小demo
- Netty自学-Netty学习(一)
- netty demo学习文章汇总
- netty 学习记录一
- Netty学习(一)
- Netty学习(一)
- Netty 实例学习一
- fourinone学习笔记一(上手demo)
- 重写JavaScript的eval和document.write方法
- 【Mybatis】Mybatis整合spring
- cf400A
- Android中Popuwindown的使用(我的项目中扣出的代码)
- opendns security blog
- Netty学习笔记一 项目demo
- 第11周 项目四-类族的设计
- Java网络编程之IP地址和InetAddress类
- cf276B
- 指针的应用
- VS2015预览版体验
- Java网络编程之统一资源定位符URL
- SNMP的应用
- performSelector的原理以及用法