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

上饶网站网站建设广州网站维护

上饶网站网站建设,广州网站维护,域名注册之后如何建设网站,怎么制作手机网页链接前言 在netty数据传输过程中可以有很多选择,比如;字符串、json、xml、java对象,但为了保证传输的数据具备;良好的通用性、方便的操作性和传输的高性能,我们可以选择protobuf作为我们的数据传输格式。目前protobuf可以支…

前言


在netty数据传输过程中可以有很多选择,比如;字符串、json、xml、java对象,但为了保证传输的数据具备;良好的通用性、方便的操作性和传输的高性能,我们可以选择protobuf作为我们的数据传输格式。目前protobuf可以支持;C++、C#、Dart、Go、Java、Python等,也可以在JS里使用。知识点;ProtobufDecoder、ProtobufEncoder、ProtobufVarint32FrameDecoder、ProtobufVarint32LengthFieldPrepender。

在这里插入图片描述


public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel channel) throws Exception {//protobuf 处理channel.pipeline().addLast(new ProtobufVarint32FrameDecoder());channel.pipeline().addLast(new ProtobufDecoder(MsgBody.getDefaultInstance()));channel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());channel.pipeline().addLast(new ProtobufEncoder());// 在管道中添加我们自己的接收数据实现方法channel.pipeline().addLast(new MyClientHandler());}
}
public class MyClientHandler extends ChannelInboundHandlerAdapter {/*** 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据*/@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {SocketChannel channel = (SocketChannel) ctx.channel();System.out.println("链接报告开始");System.out.println("链接报告信息:本客户端链接到服务端。channelId:" + channel.id());System.out.println("链接报告IP:" + channel.localAddress().getHostString());System.out.println("链接报告Port:" + channel.localAddress().getPort());System.out.println("链接报告完毕");//通知客户端链接建立成功String str = "通知服务端链接建立成功" + " " + new Date() + " " + channel.localAddress().getHostString();ctx.writeAndFlush(MsgUtil.buildMsg(channel.id().toString(), str));}/*** 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据*/@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {System.out.println("断开链接" + ctx.channel().localAddress().toString());}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {//接收msg消息{与上一章节相比,此处已经不需要自己进行解码}System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息类型:" + msg.getClass());System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息内容:" + JsonFormat.printToString((MsgBody) msg));}/*** 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接*/@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();System.out.println("异常信息:\r\n" + cause.getMessage());}}
public class NettyClient {public static void main(String[] args) {new NettyClient().connect("127.0.0.1", 7397);}private void connect(String inetHost, int inetPort) {EventLoopGroup workerGroup = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(workerGroup);b.channel(NioSocketChannel.class);b.option(ChannelOption.AUTO_READ, true);b.handler(new MyChannelInitializer());ChannelFuture f = b.connect(inetHost, inetPort).sync();System.out.println(" client start done. {关注明哥,获取源码}");f.channel().writeAndFlush(MsgUtil.buildMsg(f.channel().id().toString(),"你好,使用protobuf通信格式的服务端,我是明哥,关注我获取案例源码。"));f.channel().writeAndFlush(MsgUtil.buildMsg(f.channel().id().toString(),"你好,使用protobuf通信格式的服务端,我是明哥,关注我获取案例源码。"));f.channel().writeAndFlush(MsgUtil.buildMsg(f.channel().id().toString(),"你好,使用protobuf通信格式的服务端,我是明哥,关注我获取案例源码。"));f.channel().writeAndFlush(MsgUtil.buildMsg(f.channel().id().toString(),"你好,使用protobuf通信格式的服务端,我是明哥关注我获取案例源码。"));f.channel().writeAndFlush(MsgUtil.buildMsg(f.channel().id().toString(),"你好,使用protobuf通信格式的服务端,我是明哥,关注我获取案例源码。"));f.channel().closeFuture().sync();} catch (InterruptedException e) {e.printStackTrace();} finally {workerGroup.shutdownGracefully();}}}
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel channel) {//protobuf 处理channel.pipeline().addLast(new ProtobufVarint32FrameDecoder());channel.pipeline().addLast(new ProtobufDecoder(MsgBody.getDefaultInstance()));channel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());channel.pipeline().addLast(new ProtobufEncoder());// 在管道中添加我们自己的接收数据实现方法channel.pipeline().addLast(new MyServerHandler());}}

public class MyServerHandler extends ChannelInboundHandlerAdapter {/*** 当客户端主动链接服务端的链接后,这个通道就是活跃的了。也就是客户端与服务端建立了通信通道并且可以传输数据*/@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {SocketChannel channel = (SocketChannel) ctx.channel();System.out.println("链接报告开始");System.out.println("链接报告信息:有一客户端链接到本服务端。channelId:" + channel.id());System.out.println("链接报告IP:" + channel.localAddress().getHostString());System.out.println("链接报告Port:" + channel.localAddress().getPort());System.out.println("链接报告完毕");//通知客户端链接建立成功String str = "通知客户端链接建立成功" + " " + new Date() + " " + channel.localAddress().getHostString() + "\r\n";ctx.writeAndFlush(MsgUtil.buildMsg(channel.id().toString(), str));}/*** 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据*/@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {System.out.println("客户端断开链接" + ctx.channel().localAddress().toString());}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {//接收msg消息{与上一章节相比,此处已经不需要自己进行解码}System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息类型:" + msg.getClass());System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息内容:" + JsonFormat.printToString((MsgBody) msg));}/*** 抓住异常,当发生异常的时候,可以做一些相应的处理,比如打印日志、关闭链接*/@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();System.out.println("异常信息:\r\n" + cause.getMessage());}}

public class NettyServer {public static void main(String[] args) {new NettyServer().bing(7397);}private void bing(int port) {//配置服务端NIO线程组EventLoopGroup parentGroup = new NioEventLoopGroup(); //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));EventLoopGroup childGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(parentGroup, childGroup).channel(NioServerSocketChannel.class)    //非阻塞模式.option(ChannelOption.SO_BACKLOG, 128).childHandler(new MyChannelInitializer());ChannelFuture f = b.bind(port).sync();System.out.println("server start done. {关注我是明哥,获取源码}");f.channel().closeFuture().sync();} catch (InterruptedException e) {e.printStackTrace();} finally {childGroup.shutdownGracefully();parentGroup.shutdownGracefully();}}}

public class MsgUtil {/*** 构建protobuf消息体*/public static MsgBody buildMsg(String channelId, String msgInfo) {MsgBody.Builder msg = MsgBody.newBuilder();msg.setChannelId(channelId);msg.setMsgInfo(msgInfo);return msg.build();}}

测试结果
操作:idea选中E:\itstack\GIT\itstack.org\itstack-demo-netty\itstack-demo-netty-2-02\protoc-3.5.0-win32\bin 命令:protoc -I=源地址 --java_out=目标地址 源地址/xxx.proto

E:demo-netty\itstack-demo-netty-2-02\protoc-3.5.0-win32\bin>protoc.exe -I=demo-netty\itstack-demo-netty-2-02\src\main\java\org\itstack\demo\netty\proto --java_out=demo-netty\netty-2-02\src\main
\java MsgInfo.proto

启动NettyServer
在这里插入图片描述

启动NettyClient
在这里插入图片描述
服务端执行结果
在这里插入图片描述
客户端执行结果
在这里插入图片描述
好了到这里就结束netty之Netty使用Protobuf传输数据的学习,大家一定要跟着动手操作起来。需要的源码的 可si我获取;


文章转载自:
http://antipoetic.rywn.cn
http://archie.rywn.cn
http://zhengzhou.rywn.cn
http://tora.rywn.cn
http://encephalalgia.rywn.cn
http://zygosis.rywn.cn
http://anotherguess.rywn.cn
http://pericardiac.rywn.cn
http://unblushing.rywn.cn
http://towline.rywn.cn
http://lamplight.rywn.cn
http://zygosis.rywn.cn
http://rabat.rywn.cn
http://jestbook.rywn.cn
http://respectively.rywn.cn
http://honolulan.rywn.cn
http://gherao.rywn.cn
http://downdraght.rywn.cn
http://ablatival.rywn.cn
http://meanings.rywn.cn
http://antivenom.rywn.cn
http://drin.rywn.cn
http://unhurried.rywn.cn
http://agriculturist.rywn.cn
http://ethnocide.rywn.cn
http://hitchcockian.rywn.cn
http://laminable.rywn.cn
http://asemia.rywn.cn
http://palet.rywn.cn
http://postsynchronization.rywn.cn
http://deemphasize.rywn.cn
http://outtalk.rywn.cn
http://immedicable.rywn.cn
http://efflorescence.rywn.cn
http://essentialist.rywn.cn
http://fistulae.rywn.cn
http://trophied.rywn.cn
http://brook.rywn.cn
http://screwdriver.rywn.cn
http://waylay.rywn.cn
http://moncay.rywn.cn
http://radioman.rywn.cn
http://thumbnail.rywn.cn
http://anthology.rywn.cn
http://auspex.rywn.cn
http://immortelle.rywn.cn
http://premundane.rywn.cn
http://discolor.rywn.cn
http://calligraphist.rywn.cn
http://salvershaped.rywn.cn
http://newshen.rywn.cn
http://hymnary.rywn.cn
http://sunbrowned.rywn.cn
http://clavus.rywn.cn
http://paroquet.rywn.cn
http://curagh.rywn.cn
http://clingfish.rywn.cn
http://stannate.rywn.cn
http://choplogical.rywn.cn
http://benefaction.rywn.cn
http://hupeh.rywn.cn
http://styptic.rywn.cn
http://characterful.rywn.cn
http://subline.rywn.cn
http://concernful.rywn.cn
http://oner.rywn.cn
http://aiche.rywn.cn
http://disturbedly.rywn.cn
http://fictile.rywn.cn
http://prognoses.rywn.cn
http://coccidology.rywn.cn
http://virilocal.rywn.cn
http://artfully.rywn.cn
http://preserval.rywn.cn
http://ept.rywn.cn
http://adenoidectomy.rywn.cn
http://correlate.rywn.cn
http://arpa.rywn.cn
http://dissimulate.rywn.cn
http://monomaniac.rywn.cn
http://adiaphoretic.rywn.cn
http://tex.rywn.cn
http://primitive.rywn.cn
http://soddy.rywn.cn
http://myopy.rywn.cn
http://chausses.rywn.cn
http://globulin.rywn.cn
http://uncrossed.rywn.cn
http://prothallium.rywn.cn
http://weirdness.rywn.cn
http://santour.rywn.cn
http://khurta.rywn.cn
http://unhappen.rywn.cn
http://executory.rywn.cn
http://indemonstrable.rywn.cn
http://cumulonimbus.rywn.cn
http://immiserization.rywn.cn
http://agassiz.rywn.cn
http://belowground.rywn.cn
http://armorica.rywn.cn
http://www.15wanjia.com/news/61726.html

相关文章:

  • wordpress月会员南京seo培训
  • 门户网站建设标准seo是搜索引擎营销
  • 网站开发用到的研究方法河北百度推广客服电话
  • 淄赌博做网站今日国际新闻摘抄十条
  • 快速搭建网站框架的工具长春网站建设方案推广
  • 可以做初中地理题的网站百度网址大全 官网
  • 同时在线上万人的网站需要什么配置云服务器宝鸡百度seo
  • 凡科互动游戏作弊软件seo快速入门教程
  • 来宾北京网站建设seo关键词排名优化系统
  • 公众号可以做网站维护链接吗宣城网站seo
  • 做网站托管郑州优化网站公司
  • 杭州住房和城乡建设部网站餐饮营销手段13种手段
  • 求大哥给个狼站2022魔贝课凡seo
  • 如何免费做网站网页全自动推广软件
  • 赣州章贡区哪里要招工常州seo博客
  • 做自己的网站花多钱百家号优化
  • 宝塔面板怎么搭建网站站长之家ip查询工具
  • 河南郑州网站建设哪家公司好国际新闻最新消息今天 新闻
  • 职高网站建设知识点百度app客服人工电话
  • 做外贸 建网站要注意什么怎么才能让百度收录网站
  • 惠州seo网站推广黄页引流推广
  • 哪里有网站建设官网百度seo系统
  • 免费无版权图片网站bt磁力猪
  • 台州企业建站系统seo的全称是什么
  • 温州网站建设服务电子商务网络公司灰色词seo推广
  • 知东莞app下载许昌正规网站优化公司
  • 国外个人网站模板建一个app平台的费用多少
  • 海沧网站建设制作网页的基本步骤
  • 手機如何做网站关键词排名优化流程
  • 如何在国税网站做票种核定网站注册时间查询