前言介绍:
凡是新知识都需要有个入门的案例,一个简单的输入输出就能解除你当前遇到的所有疑惑。不要总想着先学理论后学实战,新东方还135学理论,246学实战呢【800个床位不锈钢】。
环境需求:
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
package com.itstack; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; public class ChildChannelHandler extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel e) throws Exception { System.out.println("报告"); System.out.println("信息:有一客户端链接到本服务端"); System.out.println("IP:"+e.localAddress().getHostName()); System.out.println("Port:"+e.localAddress().getPort()); System.out.println("报告完毕"); //在管道中添加我们自己的接收数据实现方法 e.pipeline().addLast(new MyServerHanlder()); } }MyServerHanlder.java
package com.itstack; import java.util.Date; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.bytes.ByteArrayDecoder; public class MyServerHanlder extends ChannelHandlerAdapter{ /* * channelAction * * channel 通道 * action 活跃的 * * 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据 * */ public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println(ctx.channel().localAddress().toString()+" channelActive"); } /* * channelInactive * * channel 通道 * Inactive 不活跃的 * * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据 * */ public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println(ctx.channel().localAddress().toString()+" channelInactive"); } /* * channelRead * * channel 通道 * Read 读 * * 简而言之就是从通道中读取数据,也就是服务端接收客户端发来的数据 * 但是这个数据在不进行解码时它是ByteBuf类型的后面例子我们在介绍 * */ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; byte[] msgByte = new byte[buf.readableBytes()]; buf.readBytes(msgByte); System.out.println(new Date()+" "+new String(msgByte,"UTF-8")); } /* * channelReadComplete * * channel 通道 * Read 读取 * Complete 完成 * * 在通道读取完成后会在这个方法里通知,对应可以做刷新操作 * ctx.flush() * */ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } /* * exceptionCaught * * exception 异常 * Caught 抓住 * * 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接 * */ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); System.out.println("异常信息:\r\n"+cause.getMessage()); } }
NettyServer .java
package com.itstack; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class NettyServer { public static void main(String[] args) { try { System.out.println("服务端开启等待客户端链接"); new NettyServer().bing(7397); } catch (Exception e) { e.printStackTrace(); } } public void bing(int port) throws Exception{ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workGroup); b.channel(NioServerSocketChannel.class); b.option(ChannelOption.SO_BACKLOG, 1024); b.childHandler(new ChildChannelHandler()); // 绑定端口 ChannelFuture f = b.bind(port).sync(); // 等待服务端监听端口关闭 f.channel().closeFuture().sync(); } finally { // 优雅的退出 bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } } }
测试运行:
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
----------------------------------------------