NIO使用总结

服务端创建关键步骤
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();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值