您的位置:首页 > 编程语言 > Java开发

Netty学习心得 netty服务端和客户端的连接

2017-10-31 12:44 399 查看
最近一直在学习Java的NIO框架 Netty,主要学习资料是李林峰老师写的 Netty权威指南 ,这本书介绍的十分详细,我也还在学习之中。第一篇就先介绍一下Netty的服务端和客户端的例子吧

虽然书中的demo都是基于Netty5的,但现在Netty5好像被丢弃啦,官网连beta版都没有,所以我还是选择啦Netty4的版本来开发

服务端主要是接受客户端传来的数据,然后在serverHandler中进行业务处理

服务端代码

public class Server {

public static void main(String[] args) {
// 服务类
ServerBootstrap b = new ServerBootstrap();

// 创建boss和worker
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

try {
// 设置循环线程组事例
b.group(bossGroup, workerGroup);

// 设置channel工厂
b.channel(NioServerSocketChannel.class);

// 设置管道
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new RequestDecoder());
ch.pipeline().addLast(new ResponseEncoder());
ch.pipeline().addLast(new ServerHandler());
}
});

b.option(ChannelOption.SO_BACKLOG, 2048);// 链接缓冲池队列大小

// 绑定端口
b.bind(10111).sync();

System.out.println("start!!!");

} catch (Exception e) {
e.printStackTrace();
} finally{
//释放资源
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}


服务端的消息处理代码

public class ServerHandler extends SimpleChannelInboundHandler<String> {

/**
* 消息处理
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg)throws Exception {

// 打印输出消息
System.out.println("服务端接受的消息 : " + msg);
// 返回客户端消息
ctx.writeAndFlush("hello");
}

/**
* 新客户端接入
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive");
super.channelActive(ctx);
}

/**
* 客户端断开
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelInactive");
super.channelInactive(ctx);
}

/**
* 异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}

}


客户端主要是向服务端发送数据,处理服务端返回的数据

客户端代码

public class Client {

public static void main(String[] args) {
//客户端服务类
Bootstrap bootstrap = new Bootstrap();

//worker
EventLoopGroup worker = new NioEventLoopGroup();

try {
//设置线程池
bootstrap.group(worker);

//设置socket工厂、
bootstrap.channel(NioSocketChannel.class);

//设置管道
bootstrap.handler(new ChannelInitializer<Channel>() {

@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ClientHandler());
}
});

ChannelFuture connect = bootstrap.connect("127.0.0.1", 10111).sync();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入:");

String msg = bufferedReader.readLine();
connect.channel().writeAndFlush(msg);
}

} catch (Exception e) {
e.printStackTrace();
} finally{
worker.shutdownGracefully();
}
}
}


客户端消息处理

public class ClientHandler extends SimpleChannelInboundHandler<String> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("客户端接受的消息: " + msg);
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive ");
super.channelActive(ctx);
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelInactive ");
super.channelInactive(ctx);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java nio
相关文章推荐