服务端
/**
* 服务端
* @author suibibk@qq.com
*/
public class NettyServer {
public static void main(String[] args) throws Exception{
//1、创建一个线程组,接收客户端连接
EventLoopGroup bossGroup = new NioEventLoopGroup();
//2、创建一个线程组,处理网络操作
EventLoopGroup workerGroup = new NioEventLoopGroup();
//3、创建服务端启动助手来配置参数
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)//4、设置两个线程组
.channel(NioServerSocketChannel.class)//5、使用NioServerSocketChannel作为服务器端通道的实现
.option(ChannelOption.SO_BACKLOG, 128)//6、设置线程队列中等待连接的个数
.childOption(ChannelOption.SO_KEEPALIVE, true)//7、保持活动连接状态
.childHandler(new ChannelInitializer<SocketChannel>() {//8、创建一个通道初始化对象
@Override
protected void initChannel(SocketChannel sc) throws Exception {
//9、往pipeline链中添加自定义的handler
sc.pipeline().addLast(new NettyServerHandler());
}
});
System.out.print("Server is ready.....");
ChannelFuture cf = b.bind(9999).sync();//10、绑定端口,bind方法是异步的,sync方法是同步阻塞的
System.out.print("Server is starting.....");
//11、关闭通道,关闭线程组
cf.channel().closeFuture().sync();//关闭连接(异步非阻塞)
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
System.out.print("Server is end.....");
}
}
记得要用
import io.netty.channel.socket.SocketChannel;
用这个会报下面的错误
import java.nio.channels.SocketChannel;
Bound mismatch: The type SocketChannel is not a valid substitute for the bounded parameter <C extends Channel> of the type ChannelInitializer<C>
服务端处理业务类
/**
* 服务端处理业务类
* @author suibibk@qq.com
*
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter{
//读取数据事件
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("Server :"+ctx);
ByteBuf buf = (ByteBuf)msg;
System.out.println("客户端发来的消息:"+buf.toString(CharsetUtil.UTF_8));
}
//数据读取完毕事件
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("疫情太严重,我没有钱", CharsetUtil.UTF_8));
}
//异常发生事件
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println(cause.getMessage());
ctx.close();
}
}
客户端
/**
* 网络客户端
* @author suibibk@qq.com
*
*/
public class NettyClient {
public static void main(String[] args)throws Exception {
//1、创建一个线程组
EventLoopGroup group = new NioEventLoopGroup();
//2、创建客户端启动助手,完成相关配置
Bootstrap b = new Bootstrap();
b.group(group)//3、设置线程组
.channel(NioSocketChannel.class)//4、设置客户端通道的实现类
.handler(new ChannelInitializer<SocketChannel>() {//创建一个初始化通道对象
@Override
protected void initChannel(SocketChannel sc) throws Exception {
//6、在pipline中添加自定义的handler
sc.pipeline().addLast(new NettyClientHandler());
}
});
System.out.println("Client is ready");
//7、启动客户端去连接服务器端 connect方法是异步的,sync方法是同步阻塞的
ChannelFuture cf =b.connect("127.0.0.2", 9999).sync();
//8、关闭连接(异步非阻塞)
cf.channel().closeFuture().sync();
System.out.print("Client is end.....");
}
}
客户端处理业务类
/**
* 客户端业务处理类
* @author suibibk@qq.com
*
*/
public class NettyClientHandler extends ChannelInboundHandlerAdapter{
//通道就绪时间
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("Client"+ctx);
ctx.writeAndFlush(Unpooled.copiedBuffer("老板还钱吧", CharsetUtil.UTF_8));
}
//读取数据事件
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf)msg;
System.out.println("服务端发来的消息:"+buf.toString(CharsetUtil.UTF_8));
}
}