个人随笔
目录
Netty框架入门案例(NIO)
2020-02-06 23:51:31

服务端

  1. /**
  2. * 服务端
  3. * @author suibibk@qq.com
  4. */
  5. public class NettyServer {
  6. public static void main(String[] args) throws Exception{
  7. //1、创建一个线程组,接收客户端连接
  8. EventLoopGroup bossGroup = new NioEventLoopGroup();
  9. //2、创建一个线程组,处理网络操作
  10. EventLoopGroup workerGroup = new NioEventLoopGroup();
  11. //3、创建服务端启动助手来配置参数
  12. ServerBootstrap b = new ServerBootstrap();
  13. b.group(bossGroup, workerGroup)//4、设置两个线程组
  14. .channel(NioServerSocketChannel.class)//5、使用NioServerSocketChannel作为服务器端通道的实现
  15. .option(ChannelOption.SO_BACKLOG, 128)//6、设置线程队列中等待连接的个数
  16. .childOption(ChannelOption.SO_KEEPALIVE, true)//7、保持活动连接状态
  17. .childHandler(new ChannelInitializer<SocketChannel>() {//8、创建一个通道初始化对象
  18. @Override
  19. protected void initChannel(SocketChannel sc) throws Exception {
  20. //9、往pipeline链中添加自定义的handler
  21. sc.pipeline().addLast(new NettyServerHandler());
  22. }
  23. });
  24. System.out.print("Server is ready.....");
  25. ChannelFuture cf = b.bind(9999).sync();//10、绑定端口,bind方法是异步的,sync方法是同步阻塞的
  26. System.out.print("Server is starting.....");
  27. //11、关闭通道,关闭线程组
  28. cf.channel().closeFuture().sync();//关闭连接(异步非阻塞)
  29. bossGroup.shutdownGracefully();
  30. workerGroup.shutdownGracefully();
  31. System.out.print("Server is end.....");
  32. }
  33. }

记得要用

  1. import io.netty.channel.socket.SocketChannel;

用这个会报下面的错误

  1. import java.nio.channels.SocketChannel;
  1. Bound mismatch: The type SocketChannel is not a valid substitute for the bounded parameter <C extends Channel> of the type ChannelInitializer<C>

服务端处理业务类

  1. /**
  2. * 服务端处理业务类
  3. * @author suibibk@qq.com
  4. *
  5. */
  6. public class NettyServerHandler extends ChannelInboundHandlerAdapter{
  7. //读取数据事件
  8. @Override
  9. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  10. System.out.println("Server :"+ctx);
  11. ByteBuf buf = (ByteBuf)msg;
  12. System.out.println("客户端发来的消息:"+buf.toString(CharsetUtil.UTF_8));
  13. }
  14. //数据读取完毕事件
  15. @Override
  16. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  17. ctx.writeAndFlush(Unpooled.copiedBuffer("疫情太严重,我没有钱", CharsetUtil.UTF_8));
  18. }
  19. //异常发生事件
  20. @Override
  21. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  22. System.out.println(cause.getMessage());
  23. ctx.close();
  24. }
  25. }

客户端

  1. /**
  2. * 网络客户端
  3. * @author suibibk@qq.com
  4. *
  5. */
  6. public class NettyClient {
  7. public static void main(String[] args)throws Exception {
  8. //1、创建一个线程组
  9. EventLoopGroup group = new NioEventLoopGroup();
  10. //2、创建客户端启动助手,完成相关配置
  11. Bootstrap b = new Bootstrap();
  12. b.group(group)//3、设置线程组
  13. .channel(NioSocketChannel.class)//4、设置客户端通道的实现类
  14. .handler(new ChannelInitializer<SocketChannel>() {//创建一个初始化通道对象
  15. @Override
  16. protected void initChannel(SocketChannel sc) throws Exception {
  17. //6、在pipline中添加自定义的handler
  18. sc.pipeline().addLast(new NettyClientHandler());
  19. }
  20. });
  21. System.out.println("Client is ready");
  22. //7、启动客户端去连接服务器端 connect方法是异步的,sync方法是同步阻塞的
  23. ChannelFuture cf =b.connect("127.0.0.2", 9999).sync();
  24. //8、关闭连接(异步非阻塞)
  25. cf.channel().closeFuture().sync();
  26. System.out.print("Client is end.....");
  27. }
  28. }

客户端处理业务类

  1. /**
  2. * 客户端业务处理类
  3. * @author suibibk@qq.com
  4. *
  5. */
  6. public class NettyClientHandler extends ChannelInboundHandlerAdapter{
  7. //通道就绪时间
  8. @Override
  9. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  10. System.out.println("Client"+ctx);
  11. ctx.writeAndFlush(Unpooled.copiedBuffer("老板还钱吧", CharsetUtil.UTF_8));
  12. }
  13. //读取数据事件
  14. @Override
  15. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  16. ByteBuf buf = (ByteBuf)msg;
  17. System.out.println("服务端发来的消息:"+buf.toString(CharsetUtil.UTF_8));
  18. }
  19. }
 245

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2