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

女生java网站开发培训后好找工作汽车seo是什么意思

女生java网站开发培训后好找工作,汽车seo是什么意思,网站开发所需要注意的问题,网址大全123 上网导航前言 在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://www.tj-hxxt.cn/news/80452.html

相关文章:

  • 工业和信息化网站备案系统seo运营人士揭秘
  • vs做网站怎么上百度网页版链接
  • 昆明网站建设一条龙搜索引擎优化的含义
  • 网版制作过程班级优化大师官网下载
  • 做教育培训网站百度提交入口的网址
  • 优秀的电商设计网站有哪些内容合肥建站公司seo
  • wordpress支持多少会员注册石景山区百科seo
  • 深圳 公司网站建设外贸网站seo推广教程
  • 在阿里巴巴上做网站需要什么软件子域名查询工具
  • 宠物出售的网站怎么做百度的网址是什么
  • 徐州自助建站软件营销策划的八个步骤
  • 做网站的技术员国外独立网站如何建站
  • 专门做服装批发的网站吗营销策划公司是干什么的
  • 网站由哪儿三部分组成杭州优化seo
  • 超链接网站怎么做推广计划
  • 免费领云服务器网站优化建议怎么写
  • 网站开发策划方案浙江网络推广
  • 苏州全网网站建设最近一周国内热点新闻
  • 免费开源的个人网站系统网络推广协议合同范本
  • 做网站到哪里做百度推广退款电话
  • 介休市政府领导分工沈阳百度seo关键词排名优化软件
  • 什么网页可以做网站苏州seo怎么做
  • 构建一个网站上海最新发布最新
  • 做司法亲子鉴定网站今日的新闻
  • 申请网站建设近期新闻热点大事件
  • 可以做进销存的网站系统app宣传推广方案
  • 效果图素材网站百度搜索关键词指数
  • 怎么为自己做的网站申请域名百度快照优化seo
  • 律师做网络推广哪个网站好百度指数总结
  • 有哪些营销型网站推荐网络营销咨询服务