TCP/IP学习笔记五:Netty使用–简单通信编程1
标签(空格分隔):Netty 网络编程
Netty的简单使用示例。
编程思路按照注释进行就可以了。
一、导入Netty的jar
最先版本:netty-all-5.0.0.Alpha2.jar
二、服务器端
package com.netty.demo1.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* Netty NIO 服务器端
* @author MOTUI
*
*/
public class NettyNIOServer {
public static void main(String[] args) throws InterruptedException {
//1.创建NIOServerSocketChannel的服务引导
ServerBootstrap serverBootstrap = new ServerBootstrap();
//2.创建线程池 boss(请求转发) worker(IO事件处理)
NioEventLoopGroup boss = new NioEventLoopGroup();
NioEventLoopGroup worker = new NioEventLoopGroup();
//3.绑定线程
serverBootstrap.group(boss, worker);
//4.设置ServerSocket服务类
serverBootstrap.channel(NioServerSocketChannel.class);
//5.绑定IO处理事件
serverBootstrap.childHandler(new ServerChannelInitializer());
//6.绑定服务端口
System.out.println("服务器监听8989端口");
ChannelFuture future = serverBootstrap.bind(8989).sync();
//等待服务器被关闭
future.channel().closeFuture().sync();
//释放线程资源
worker.shutdownGracefully();
boss.shutdownGracefully();
}
}
注册IO事件处理类
package com.netty.demo1.server;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
/**
* 注册IO事件处理类
*/
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerRequestResponseHander());
}
}
IO事件处理类
package com.netty.demo1.server;
import java.util.Date;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
/**
* 事件处理类
* @author MOTUI
*
*/
public class ServerRequestResponseHander extends ChannelHandlerAdapter {
/**
* 异常调用
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.out.println("发生异常了···异常信息:"+cause.getMessage());
}
/**
* 读数据调用
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
ByteBuf buffer=(ByteBuf) msg;
System.out.println("服务器接收到的数据:"+buffer.toString(CharsetUtil.UTF_8));
//ctx.writeAndFlush(msg);//写回响应
//响应一个时间
ByteBuf buf=ctx.alloc().buffer(1024);
buf.writeBytes(new Date().toString().getBytes());
ctx.writeAndFlush(buf);
}
}
客户端
package com.netty.demo1.client;
import java.net.InetSocketAddress;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* Netty NIO 客户端
* @author MOTUI
*
*/
public class NettyNIOClient {
public static void main(String[] args) throws InterruptedException {
//1.创建NIOSocketChannel的服务引导
Bootstrap bootstrap = new Bootstrap();
//2.创建线程池 worker(IO事件处理)
NioEventLoopGroup boss = new NioEventLoopGroup();
//3.关联线程池
bootstrap.group(boss);
//4.设置NioSocketChannel
bootstrap.channel(NioSocketChannel.class);
//5.绑定IO处理事件
bootstrap.handler(new ClientChannelInitializer());
//6.链接服务器
ChannelFuture future = bootstrap.connect(new InetSocketAddress("192.168.0.117", 8989));
//等待关闭连接
future.channel().closeFuture().sync();
//释放资源
boss.shutdownGracefully();
}
}
客户端注册IO事件处理类
package com.netty.demo1.client;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
/**
* 注册IO事件处理类
*/
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ClientRequestResponseHander());
}
}
客户端IO事件处理类
package com.netty.demo1.client;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
/**
* 事件处理类
* @author MOTUI
*
*/
public class ClientRequestResponseHander extends ChannelHandlerAdapter {
/**
* 异常调用
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.out.println("发生异常了···异常信息:"+cause.getMessage());
}
/**
* 发送请求
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//创建ByteBuf
ByteBuf buf = Unpooled.buffer(1024);
buf.writeBytes("你好,我好,大家好!".getBytes());
ctx.writeAndFlush(buf);
}
/**
* 读数据调用
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
ByteBuf buffer=(ByteBuf) msg;
System.out.println("客户端收到:"+buffer.toString(CharsetUtil.UTF_8));
//关闭链接
ctx.close();
}
}
总结:
编程思路按照官方文档进行编码,简单类型的传递没有太大的问题,对于对象类型数据的传输会存在一些问题(下篇介绍对象类型的传输)。