netty基础教程-4、helloworld(优化版)
程序员文章站
2022-03-21 13:56:00
...
功能实现
服务端
- 接受客户端数据
- 响应结果给客户端
客户端
- 发送请求给服务端
- 接受服务端的响应
服务端实现
启动类:Server.java
public class Server {
public static void main(String[] args) throws Exception{
//创建两个组,分别用来接受客户端情求和处理客户端消息的
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
//配置服务端
ChannelFuture cf = b.group(bossGroup,workGroup).channel(NioServerSocketChannel.class)
//1)
.childHandler(new ServerChannelInit())
.bind(8899)
.sync();
System.out.println("服务端初始化完成!!!");
//最后关闭服务端资源
cf.channel().closeFuture().sync();
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
System.out.println("服务端停止服务!!!");
}
}
1)、在这里将业逻辑注入,这样就很好的将业务和IO操作分离开了
通道初始化类:ServerChannelInit.java
public class ServerChannelInit extends ChannelInitializer<NioSocketChannel> {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//对服务端管道进行配置,编解码,Handler的处理顺序等等
ch.pipeline()
.addLast(new StringEncoder())
.addLast(new StringDecoder())
.addLast(new ServerHandler());
}
}
通道数据处理类:ServerHandler.java
public class ServerHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("接受到数据是:"+msg);
//返回响应
ctx.writeAndFlush("服务端已经接受数据!!");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("=============test通道**============");
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("=============test通道**ing============");
super.channelInactive(ctx);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("=============test通道数据读取完毕!!============");
super.channelReadComplete(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("=============test通道**出现异常============");
super.exceptionCaught(ctx, cause);
ctx.close();
}
}
客户端实现
启动类:Client.java
public class Client {
public static void main(String[] args) throws Exception{
NioEventLoopGroup workGroup = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
ChannelFuture cf = b.group(workGroup)
.channel(NioSocketChannel.class)
.handler(new ClientChannelInit())
.connect("127.0.0.1", 8899)
.sync();
server",CharsetUtil.UTF_8));
System.out.println("客户端初始化完成!!!!");
cf.channel().closeFuture().sync();
}
}
通道初始化类:ClientChannelInit.java
public class ClientChannelInit extends ChannelInitializer<NioSocketChannel> {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new StringEncoder())
.addLast(new StringDecoder())
.addLast(new ClientHandler());
}
}
通道数据处理类:ClientHandler.java
public class ClientHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("接受到服务端的消息:"+msg);
}
/**
* 在channel初始化的时候,发送数据
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("hello server");
}
}
总结
1、这里使用了SimpleChannelInboundHandler(netty提供的类),这个使得我们操作数据更加简便,传入的泛型就是数据的类型,并且帮我们管理好了资源问题
2、使用了StringDecoder 和 StringEncoder类,在底层帮我们处理好了数据的转换,这样我们就能更加专注的处理handler中的业务逻辑了。
github源码: https://github.com/wcjwctwy/netty-study
上一篇: Matplotlib绘图工具(三)
下一篇: python 绘制动态二维码