一:netty介绍
Netty是由Jboss提供的一个java 开源框架,github上独立运行的项目。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
二使用:添加pom文件
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.64.Final</version>
</dependency>
三 idea 开发工具
服务端
package com.persom.test;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @Auther yuanling
* @Date 2021/8/8
*/
public class NettyService {
public static void main(String[] args) throws Exception {
initNetty();
}
private static void initNetty() throws Exception{
//创建2个工作线程组 bossGroup 负责客服端的连接, workGroup 负责网络数据的读写
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
//创建服务器端的启动对象,配置参数
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128) //设置线程队列得到的连接数
.childOption(ChannelOption.SO_KEEPALIVE,true) //设置活动保持连接状态
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyServerHandler());
}
}) ;
System.out.println("服务端已经准备好了===");
//绑定一个端口并且同步,生成一个channelFuture 对象
ChannelFuture cf = bootstrap.bind(9988).sync();
cf.channel().closeFuture().sync();
}
}
务端handler
package com.persom.test;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* @Auther yuanling
* @Date 2021/8/8
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf= (ByteBuf) msg;
System.out.println("服务端收到客户端的消息"+buf.toString(CharsetUtil.UTF_8));
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
ctx.writeAndFlush(Unpooled.copiedBuffer("服务端返回消息",CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
ctx.close();
}
}
客户端
package com.persom.test;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioDatagramChannel;
/**
* @Auther yuanling
* @Date 2021/8/8
*/
public class ClientNetty {
public static void main(String[] args) throws Exception {
initClient();
}
private static void initClient() throws Exception{
EventLoopGroup eventGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap=new Bootstrap();
bootstrap.group(eventGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ClientHandler());
}
})
;
System.out.println("客户端启动了===");
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9988).sync();
channelFuture.channel().closeFuture().sync();
} finally {
eventGroup.shutdownGracefully();
}
}
}
客户端handler
package com.persom.test;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* @Auther yuanling
* @Date 2021/8/9
*/
public class ClientHandler extends ChannelInboundHandlerAdapter {
/**
* 当通道就绪 就会触发该方法
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
ctx.writeAndFlush(Unpooled.copiedBuffer("服务端你好", CharsetUtil.UTF_8));
}
/**
* 当通道有读取事件时会触发
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// super.channelRead(ctx, msg);
ByteBuf buf= (ByteBuf) msg;
System.out.println("客户端接收到信息"+buf.toString(CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}