Java NIO框架Netty简单使用 http://blog.csdn.net/anxpp/article/details/52108238
关闭
anxpp的博客
As they sow , so let them reap .
分类:
版权声明:http://blog.csdn.net/anxpp
转载请注明出处:http://blog.csdn.net/anxpp/article/details/52108238,谢谢!
之前写了一篇文章:Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码),介绍了如何使用Java原生IO支持进行网络编程,本文介绍一种更为简单的方式,即Java NIO框架。
Netty是业界最流行的NIO框架之一,具有良好的健壮性、功能、性能、可定制性和可扩展性。同时,它提供的十分简单的API,大大简化了我们的网络编程。
同Java IO介绍的文章一样,本文所展示的例子,实现了一个相同的功能。
1、服务端
Server:
- package com.anxpp.io.calculator.netty;
- 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;
- public class Server {
- private int port;
- public Server(int port) {
- this.port = port;
- }
- public void run() throws Exception {
- EventLoopGroup bossGroup = new NioEventLoopGroup();
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .option(ChannelOption.SO_BACKLOG, 1024)
- .childOption(ChannelOption.SO_KEEPALIVE, true)
- .childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ch.pipeline().addLast(new ServerHandler());
- }
- });
- ChannelFuture f = b.bind(port).sync();
- System.out.println("服务器开启:"+port);
- 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 = 9090;
- }
- new Server(port).run();
- }
- }
ServerHandler:
- package com.anxpp.io.calculator.netty;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- import java.io.UnsupportedEncodingException;
- import com.anxpp.io.utils.Calculator;
- public class ServerHandler extends ChannelInboundHandlerAdapter {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {
- ByteBuf in = (ByteBuf) msg;
- byte[] req = new byte[in.readableBytes()];
- in.readBytes(req);
- String body = new String(req,"utf-8");
- System.out.println("收到客户端消息:"+body);
- String calrResult = null;
- try{
- calrResult = Calculator.Instance.cal(body).toString();
- }catch(Exception e){
- calrResult = "错误的表达式:" + e.getMessage();
- }
- ctx.write(Unpooled.copiedBuffer(calrResult.getBytes()));
- }
- @Override
- public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
- ctx.flush();
- }
- /**
- * 异常处理
- */
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- cause.printStackTrace();
- ctx.close();
- }
- }
2、客户端
Client:
- <li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(153, 153, 153); border-image: initial; list-style-type: decimal-leading-zero; list-style-image: initial; color: rgb(238, 238,%