当前位置: 首页 > news >正文

扬州市城乡建设局网站首页江苏又一地检测出阳性

扬州市城乡建设局网站首页,江苏又一地检测出阳性,营销案例100例小故事及感悟,镇江市建设工程管理处网站文章目录 前言服务端相关配置核心代码 客户端 前言 前言 环境#xff1a; JDK#xff1a;64位 Jdk1.8 SpringBoot#xff1a;2.1.7.RELEASE Netty#xff1a;4.1.39.Final 功能#xff1a; 使用Netty监听端口接受客户端的数据#xff0c;并发送数据给客户端。 服务端 … 文章目录 前言服务端相关配置核心代码 客户端 前言 前言 环境 JDK64位 Jdk1.8 SpringBoot2.1.7.RELEASE Netty4.1.39.Final 功能 使用Netty监听端口接受客户端的数据并发送数据给客户端。 服务端 相关配置 pom.xml dependencygroupIdio.netty/groupIdartifactIdnetty-all/artifactIdversion4.1.39.Final/version/dependencyapplication.yml netty-socket:port: 9992bufferSize: 2048配置类 import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;/*** author qf* since 2024/10/08 21:08*/ Component ConfigurationProperties(prefix netty-socket) Data public class SocketConfig {private Integer port;private Integer bufferSize; } 核心代码 CommandLineRunner 当应用程序启动时CommandLineRunner 接口的实现类中的 run 方法会被调用 import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;/*** author qf* since 2024/10/08 21:11*/ Slf4j Component public class CommandLineRunnerImpl implements CommandLineRunner {Autowiredprivate NettyServer nettyServer;Overridepublic void run(String... args) throws Exception {log.info(-----------监听端口启动成功-----------);nettyServer.start(); // 启动Netty服务} }服务类 import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.LineBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;/*** author qf* since 2024/10/08 21:13*/ Slf4j Component public class NettyServer {Autowiredprivate SocketConfig socketConfig;public void start() throws Exception {EventLoopGroup bossGroup new NioEventLoopGroup(1);EventLoopGroup workerGroup new NioEventLoopGroup();try {Integer bufferSize socketConfig.getBufferSize();ServerBootstrap bootstrap new ServerBootstrap();bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(bufferSize, bufferSize, bufferSize)).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializerSocketChannel() {Overridepublic void initChannel(SocketChannel ch) throws Exception {//\n和\r\n分隔符ch.pipeline().addLast(new LineBasedFrameDecoder(bufferSize));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());// 添加自定义的业务处理器ch.pipeline().addLast(new DeviceServerHandler());}});ChannelFuture channelFuture bootstrap.bind(socketConfig.getPort()).sync();log.info(启动TCP服务器启动成功,正在监听端口:{}, socketConfig.getPort());channelFuture.channel().closeFuture().sync();} catch (Exception e) {log.info(netty发生错误, e);} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}} }业务处理器 import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import lombok.extern.slf4j.Slf4j;/*** author qf* since 2024/10/08 21:15*/ Slf4j public class DeviceServerHandler extends SimpleChannelInboundHandlerString {Overrideprotected void channelRead0(ChannelHandlerContext ctx, String dateStr) throws Exception {// 处理接收到的消息// 你可以在这里添加更复杂的业务逻辑比如解析消息、访问数据库等。log.info(监听的数据为------- dateStr);if (true) {//发送数据给客户端ctx.writeAndFlush(hello~);}}}客户端 import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringEncoder;import java.net.InetSocketAddress;/*** author qf* since 2024/10/11 18:01*/ public class HelloClient {public static void main(String[] args) throws InterruptedException {// 1. 启动类启动客户端new Bootstrap()// 2. 添加 EventLoop.group(new NioEventLoopGroup())//如果服务器端发来数据客户端的EventLoop就可以从selector触发读事件进行处理// 3. 选择客户端 channel 实现底层封装了SocketChannel.channel(NioSocketChannel.class)// 4. 添加处理器.handler(new ChannelInitializerNioSocketChannel() {Override // 在连接建立后被调用protected void initChannel(NioSocketChannel ch) throws Exception {ch.pipeline().addLast(new StringEncoder());//编码器将字符串编码成ByteBuf进行发送ch.pipeline().addLast(new EchoClientHandler());}})// 5. 连接到服务器.connect(new InetSocketAddress(localhost, 9992)).sync()//sync()是一个阻塞方法只有连接建立后才会继续执行.channel()//.channel()表示拿到服务器和客户端之间的SocketChannel(连接对象)// 6. 向服务器发送数据.writeAndFlush(hello, world\n);} }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;/*** author qf* since 2024/10/11 18:05*/ public class EchoClientHandler extends ChannelInboundHandlerAdapter {private final ByteBuf message;public EchoClientHandler() {message Unpooled.buffer(256);message.writeBytes(hello netty.getBytes(CharsetUtil.UTF_8));}Overridepublic void channelActive(ChannelHandlerContext ctx) {ctx.writeAndFlush(message);}Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) {String data ((ByteBuf) msg).toString(CharsetUtil.UTF_8);System.out.println(data); // ctx.write(msg);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}Overridepublic void channelReadComplete(ChannelHandlerContext ctx) {ctx.flush();}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {// Close the connection when an exception is raised.cause.printStackTrace();ctx.close();} }相关文章 SpringBoot项目监听端口接受数据(NIO版)
http://www.w-s-a.com/news/682754/

相关文章:

  • 西宁市精神文明建设网站装饰设计甲级资质
  • 做教育行业营销类型的网站徐州做网站多少钱
  • 临沂品牌网站制作企业网站建设搜集资料
  • wordpress注册验证码手机网站优化
  • 往建设厅网站上传东西做衣服的教程网站有哪些
  • 网上商城网站设计免费咨询口腔科医生回答在线
  • 南京网站c建设云世家 s浏览器
  • 如何做镜像别人网站wordpress菜单对齐修改
  • 长春网站建设net企业公示信息查询官网
  • 金鹏建设集团网站可在哪些网站做链接
  • 电子产品网站开发背景网站关键词优化方案
  • 建网站论坛wordpress提交数据库错误
  • 国内网站建设公司开源网站系统
  • 网站开发公司上大连网站建设流程图
  • 银川网站seo宁波网
  • 个人备案网站会影响吗网站添加 备案
  • 网站建设与电子商务的教案关于旅游网站建设的方案
  • 电子商务网站建设设计原则找做网站找那个平台做
  • 天津高端品牌网站建设韶关网站建设墨子
  • Wordpress多站点为什么注册不了2008iis搭建网站
  • 天津高端网站制作建网站的公司服务
  • 温州网站推广优化类似淘宝的网站怎么做的
  • 网站建设实训考试什么网站做玩具的比较多
  • 上海网站建设特点怎样给公司做一个网站做推广
  • 流量网站怎么做的济南优化排名公司
  • 保定网站制作套餐设计师导航网站大全
  • 惠州 商城网站建设石家庄新闻广播在线收听
  • 洪山网站建设域名购买之后怎么做网站
  • 北京网站建设公司服务哪家好wap是什么意思?
  • 怎么看公司网站做的好不好哦wordpress页面目录下