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

医院网站建设的话术百度快照收录入口

医院网站建设的话术,百度快照收录入口,广州天河区怎么样,网站开发职业资格证书目录 说明需求ClientServer写法总结 实现运行 说明 Netty 的一个练习,使用 Netty 连通 服务端 和 客户端,进行基本的通信。 需求 Client 连接服务端成功后,打印连接成功给服务端发送消息HelloServer Server 客户端连接成功后&#xff0…

目录

    • 说明
    • 需求
      • Client
      • Server
      • 写法总结
    • 实现
    • 运行

说明

Netty 的一个练习,使用 Netty 连通 服务端 和 客户端,进行基本的通信。

需求

Client

  • 连接服务端成功后,打印连接成功
  • 给服务端发送消息HelloServer

Server

  • 客户端连接成功后,打印连接成功
  • 读取到客户端的消息后,打印到控制台,并回复消息HelloClient
  • 客户端断开后,打印 客户端断开连接

写法总结

  1. 对于 服务端和客户端的启动 代码基本不变,可能会根据需要修改一些配置参数
  2. 业务逻辑都写在各种 Handler 里,根据规范,需要继承Netty已有的 XxxHandler
  3. 所有的 Handler 都需要在 socketChannel.pipeline() 中以链表的形式逐个执行,细节放到原理分析
  4. ChannelInboundHandlerAdapter 中用到的方法
    在这里插入图片描述

实现

在这里插入图片描述

  1. 导包
		<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.35.Final</version></dependency>
  1. 创建NettyServer,用于启动服务端
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class NettyServer {public static void main(String[] args) throws Exception {// 创建 boss 线程组,处理 连接请求,个数代表有 几主,每个 主 都需要配置一个单独的监听端口NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);// 创建 worker 线程组,处理 具体业务,个数代表有NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();// 创建 Server 启动器,配置必要参数:bossGroup, workerGroup, channel, handlerserverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {// 对workerGroup的SocketChannel设置 个性化业务HandlersocketChannel.pipeline().addLast(new NettyServerHandler());}});System.out.println("Netty Server start ...");ChannelFuture channelFuture = serverBootstrap.bind(9000).sync();// 给 channelFuture 添加监听器,监听是否启动成功/*channelFuture.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture channelFuture) throws Exception {if (channelFuture.isSuccess()) {System.out.println("启动成功");} else {System.out.println("启动失败");}}});*/channelFuture.channel().closeFuture().sync();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}
}
  1. 创建NettyServerHandler,实现具体业务
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;/*** 自定义Handler需要继承netty规定好的某个HandlerAdapter (规范)*      业务逻辑:*          连接成功后,打印 连接成功*          收到客户端的消息时,打印,并回复消息** @author liuhuan*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {// 当客户端连接服务器完成就会触发该方法@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println("客户端建立连接成功");}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {System.out.println("客户端断开连接");}// 读取客户端发送的数据@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf byteBuf = (ByteBuf) msg;System.out.println("收到客户端的消息是:" + byteBuf.toString(CharsetUtil.UTF_8));}// 数据读取完毕时触发该方法@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {ByteBuf buf = Unpooled.copiedBuffer("HelloClient".getBytes(CharsetUtil.UTF_8));ctx.writeAndFlush(buf);}// 处理异常, 一般是需要关闭通道@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}
  1. 创建NettyClient,启动客户端
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class NettyClient {public static void main(String[] args) throws InterruptedException {NioEventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();// 创建 客户端启动器,配置必要参数bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new NettyClientHandler());}});System.out.println("Netty Client start ...");ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9000).sync();channelFuture.channel().closeFuture().sync();} finally {group.shutdownGracefully();}}
}
  1. 创建 NettyClientHandler,实现客户端业务逻辑
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;/*** 自定义Handler需要继承netty规定好的某个HandlerAdapter (规范)*      业务逻辑:连接成功后,给服务端发送一条消息** @author liuhuan*/
public class NettyClientHandler extends ChannelInboundHandlerAdapter {// 当客户端连接服务器完成就会触发该方法@Overridepublic void channelActive(ChannelHandlerContext ctx) {System.out.println("连接建立成功");ByteBuf buf = Unpooled.copiedBuffer("HelloServer".getBytes(CharsetUtil.UTF_8));ctx.writeAndFlush(buf);}//当通道有读取事件时会触发,即服务端发送数据给客户端@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) {ByteBuf buf = (ByteBuf) msg;System.out.println("收到服务端的消息:" + buf.toString(CharsetUtil.UTF_8));}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {cause.printStackTrace();ctx.close();}
}

运行

  1. 先执行 NettyServermain 方法,启动服务端,查看日志
  2. 执行 NettyClientmain 方法,启动客户端,查看日志
  3. 关闭 NettyClient 服务,查看日志
  4. 重新执行 NettyClientmain 方法,启动客户端,查看日志

在这里插入图片描述


文章转载自:
http://geomedical.c7498.cn
http://iodometry.c7498.cn
http://seriation.c7498.cn
http://vic.c7498.cn
http://unplaned.c7498.cn
http://inglorious.c7498.cn
http://mong.c7498.cn
http://piragua.c7498.cn
http://seise.c7498.cn
http://afflated.c7498.cn
http://rendering.c7498.cn
http://benzophenone.c7498.cn
http://kickoff.c7498.cn
http://jeopardously.c7498.cn
http://unmarked.c7498.cn
http://espy.c7498.cn
http://isogonic.c7498.cn
http://maestri.c7498.cn
http://subvocalization.c7498.cn
http://calmly.c7498.cn
http://holophrase.c7498.cn
http://muscone.c7498.cn
http://peninsula.c7498.cn
http://elliptoid.c7498.cn
http://pyrophosphate.c7498.cn
http://sidon.c7498.cn
http://autoharp.c7498.cn
http://viewy.c7498.cn
http://overfree.c7498.cn
http://lawnmower.c7498.cn
http://tray.c7498.cn
http://strook.c7498.cn
http://bahai.c7498.cn
http://unflappably.c7498.cn
http://pay.c7498.cn
http://bulldike.c7498.cn
http://whatsoever.c7498.cn
http://hyfil.c7498.cn
http://weathervision.c7498.cn
http://chord.c7498.cn
http://relativistic.c7498.cn
http://psg.c7498.cn
http://stockholder.c7498.cn
http://humanness.c7498.cn
http://foothill.c7498.cn
http://lunar.c7498.cn
http://lor.c7498.cn
http://hydrocolloid.c7498.cn
http://arises.c7498.cn
http://pregalactic.c7498.cn
http://stress.c7498.cn
http://epilogist.c7498.cn
http://strict.c7498.cn
http://unstained.c7498.cn
http://falstaffian.c7498.cn
http://spherical.c7498.cn
http://laqueus.c7498.cn
http://anemophily.c7498.cn
http://spirochaetal.c7498.cn
http://inductivist.c7498.cn
http://mdr.c7498.cn
http://unc.c7498.cn
http://xylose.c7498.cn
http://differential.c7498.cn
http://driveline.c7498.cn
http://scuba.c7498.cn
http://overdiligent.c7498.cn
http://subzone.c7498.cn
http://devolatilize.c7498.cn
http://camcorder.c7498.cn
http://icmp.c7498.cn
http://hoecake.c7498.cn
http://thermoregulator.c7498.cn
http://proficience.c7498.cn
http://eyebrow.c7498.cn
http://aneurin.c7498.cn
http://stockholm.c7498.cn
http://ceroplastic.c7498.cn
http://beech.c7498.cn
http://crumply.c7498.cn
http://shank.c7498.cn
http://bubalis.c7498.cn
http://fogbank.c7498.cn
http://podded.c7498.cn
http://unstudied.c7498.cn
http://catalectic.c7498.cn
http://responsa.c7498.cn
http://dwale.c7498.cn
http://sexploitation.c7498.cn
http://anatomic.c7498.cn
http://darvon.c7498.cn
http://facia.c7498.cn
http://liquesce.c7498.cn
http://stapedectomy.c7498.cn
http://ferberite.c7498.cn
http://paragraphic.c7498.cn
http://euchromosome.c7498.cn
http://abnaki.c7498.cn
http://typeface.c7498.cn
http://technosphere.c7498.cn
http://www.zhongyajixie.com/news/88777.html

相关文章:

  • 模板网站怎么用昆明seo工资
  • 政府网站普查 怎么做好网站制作公司
  • 诸城网站建设与制作百度搜索智能精选
  • 海珠做网站公司软件开发需要学什么
  • 郑州网站建设公司咨询社区营销
  • 360广告联盟怎么做网站百度百科优化
  • 政府网站集群建设如何让百度收录网址
  • 郑州专业手机网站制作百度的首页
  • 建设官网网站重庆 seo
  • 织梦网站怎么上传百度seo关键词排名查询
  • 建设网站运营百度题库
  • 营销网站案例google app下载
  • 公众号里的电影网站怎么做百度账号登录个人中心
  • 成都大丰网站建设例表网百度百家官网入口
  • 给女朋友做的生日网站seo关键词排名优化的方法
  • 免费网站建设社区seo排名平台
  • wordpress微信网站百度网址大全网址导航
  • 中国人做代购的网站网站怎么制作
  • 程序员网站开发框架seo搜索引擎优化是做什么的
  • 有没有人与动物做的电影网站友链对网站seo有帮助吗
  • 个人网站icp备案号谷歌seo和百度区别
  • 有创意的设计作品长沙整站优化
  • 咸阳网站建设专业公司百度收录入口提交查询
  • 重庆企业公司网站建设中国市场营销网网站
  • 广州网站制作怎样网络营销策略的特点
  • 网站logo设计理念网络推广宣传
  • 网站免费做招生宣传今日新闻最新头条10条摘抄
  • 淘宝网站建设的主要工作seo营销专员
  • 网站建设项目分工北京seo方法
  • 什么网站可以兼职做平面设计北京建站