服务端创建关键步骤
1) 创建服务端启动类对象(ServerBootstrap)
2) 设置线程组(Boss 线程组和 Worker 线程组)
3) 设置服务端 channel 对象(NioServerSocketChannel)
4) 设置 ChanelHandler 对象
5) 绑定并启动端口监听(等待客户端链接)
2. 服务端代码实现
代码如下:
创建事件服务器
public class TimeServer {
public void start() throws Exception{
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
ServerBootstrap b=new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeServerHandler());
};
})
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f=b.bind(9999).sync();
f.channel().closeFuture().sync();
}finally{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args)throws Exception {
new TimeServer().start();
}
}
创建服务端时间处理器
代码如下:
public class TimeServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(final ChannelHandlerContext ctx)
throws Exception {
final ByteBuf time=ctx.alloc().buffer(8);
time.writeLong(System.currentTimeMillis());
final ChannelFuture f=ctx.writeAndFlush(time);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
assert f == future;
ctx.close();
}
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
{
cause.printStackTrace();
ctx.close();
}
}
Netty 客户端入门实现
1. 客户端创建关键步骤
1) 创建服务端启动类对象(Bootstrap)
2) 设置线程组(Worker 线程组)
3) 设置客户端 channel 对象(NioSocketChannel)
4) 设置 ChanelHandler 对象
5) 连接服务端
2. 客户端代码实现
创建时间客户端
public class TimeClient {
public void connect(String ip,Integer port)throws Exception {
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
//Bootstrap客户端用于简单建立Channel
Bootstrap b=new Bootstrap();
b.group(workerGroup);
//NioSocketChannel用于客户端创建Channel
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE,true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
//指定使用的数据处理方式
ch.pipeline().addLast(new TimeClientHandler());
};
});
//客户端开始连接
ChannelFuture f=b.connect(ip,port).sync();
//等待直到这个连接被关闭
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args)throws Exception {
new TimeClient().connect("127.0.0.1", 9999);
}
}
创建客户端时间处理器
public class TimeClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
ByteBuf m=(ByteBuf)msg;
try {
long currentTimeMillis = m.readLong();
System.out.println("from server:"+new Date(currentTimeMillis));
ctx.close();
} finally {
m.release();
}
}
}