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

定制网页设计报价上海网站营销seo方案

定制网页设计报价,上海网站营销seo方案,交互式网站公安备案怎么做,长沙网站建设市场低价目录一、服务端1、主程序类2、自定义初始化器3、自定义处理器二、客户端1、主程序类2、自定义初始化器3、自定义处理器三、启动服务端、客户端1、服务端:你好,我是服务端,哪吒编程2、客户端:我去,还真连上了&#xff0…

目录

    • 一、服务端
      • 1、主程序类
      • 2、自定义初始化器
      • 3、自定义处理器
    • 二、客户端
      • 1、主程序类
      • 2、自定义初始化器
      • 3、自定义处理器
    • 三、启动服务端、客户端
      • 1、服务端:你好,我是服务端,哪吒编程
      • 2、客户端:我去,还真连上了,第一次使用Netty通话,真神奇
      • 3、服务端:土包子
      • Java高并发编程实战系列文章
      • 哪吒精品系列文章

在 Netty网络编程实战1,搭建第一个Netty服务器中,使用curl作为客户端访问,下面将通过Netty实现客户端,客户端代码依然采用Netty老套路 主程序类+自定义初始化器+自定义处理器三部分组成。

一、服务端

1、主程序类

package com.guor.demo.netty.chat;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class MyNettyServerTest {public static void main(String[] args) throws InterruptedException {/*** EventLoopGroup:事件循环组,是一个线程池,也是一个死循环,用于不断地接收用户请求;* serverGroup:用户监听及建立连接,并把每一个连接抽象为一个channel,最后再将连接交给clientGroup处理;* clientGroup:真正的处理连接*/EventLoopGroup serverGroup = new NioEventLoopGroup();EventLoopGroup clientGroup = new NioEventLoopGroup();try {// 服务端启动时的初始化操作ServerBootstrap serverBootstrap = new ServerBootstrap();// 1、将serverGroup和clientGroup注册到服务端的Channel上;// 2、注册一个服务端的初始化器MyNettyServerInitializer;// 3、该初始化器中的initChannel()方法会在连接被注册到Channel后立刻执行;// 5、最后将端口号绑定到8080;ChannelFuture channelFuture = serverBootstrap.group(serverGroup, clientGroup).channel(NioServerSocketChannel.class).childHandler(new MyNettyServerInitializer()).bind(8080).sync();channelFuture.channel().closeFuture().sync();}catch (Exception e){System.out.println(e);}finally {serverGroup.shutdownGracefully();clientGroup.shutdownGracefully();}}
}

2、自定义初始化器

package com.guor.demo.netty.chat;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;/*** 自定义初始化器*/
public class MyNettyServerInitializer extends ChannelInitializer<SocketChannel> {// 连接被注册到Channel后,立刻执行此方法@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline();/*** LengthFieldBasedFrameDecoder用于解析带固定长度的数据报。* TCP发送的数据规则:可以将数据进行拆分或合并,因此对端接收到的数据报可能不是初始发送时的格式;* 一般的做法是在包头设置length长度,指明数据包的长度,再由接受方根据length拼接或剪裁收到的数据,从而形成完整的数据包*/pipeline.addLast("LengthFieldBasedFrameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,8,0,8));// 将上条语句的length加入传递的数据中心pipeline.addLast("LengthFieldPrepender",new LengthFieldPrepender(8));// 传递字符串的编码解码器pipeline.addLast("StringDecoder",new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("StringEecoder",new StringEncoder(CharsetUtil.UTF_8));// 增加自定义处理器MyNettyServerHandler,用于实际处理请求,并给出响应pipeline.addLast("MyNettyServerHandler",new MyNettyServerHandler());}
}

3、自定义处理器

package com.guor.demo.netty.chat;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;import java.util.Scanner;/*** 自定义处理器* Inbound代表"进入"的请求*/
public class MyNettyServerHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String receiveMsg) throws Exception {// 通过ctx获取客户端的IP和端口号,并打印出客户端发来的消息System.out.println("服务端接收的请求来自:"+ctx.channel().remoteAddress()+",消息内容:"+receiveMsg);System.out.println("请向客户端发送一条消息:");String sendMsg = new Scanner(System.in).nextLine();ctx.channel().writeAndFlush(sendMsg);}
}

二、客户端

1、主程序类

package com.guor.demo.netty.chat;import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;/*** 主程序类*/
public class MyNettyClientTest {public static void main(String[] args) {/*** 服务端有两个EventLoopGroup,serverGroup用于获取连接并将连接分发给clientGroup,clientGroup负责处理连接;* 对于客户端而言,客户端仅仅需要连接服务端即可*/EventLoopGroup eventLoopGroup = new NioEventLoopGroup();try {// 客户端启动时的初始化操作Bootstrap bootstrap = new Bootstrap();bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new MyNettyClientInitializer());ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();channelFuture.channel().closeFuture().sync();}catch (Exception e){System.out.println(e);}finally {eventLoopGroup.shutdownGracefully();}}
}

2、自定义初始化器

package com.guor.demo.netty.chat;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;/*** 自定义初始化器*/
public class MyNettyClientInitializer extends ChannelInitializer<SocketChannel> {// 连接被注册后,立即执行此方法@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline();/*** LengthFieldBasedFrameDecoder用于解析带固定长度的数据报。* TCP发送的数据规则:可以将数据进行拆分或合并,因此对端接收到的数据报可能不是初始发送时的格式;* 一般的做法是在包头设置length长度,指明数据包的长度,再由接受方根据length拼接或剪裁收到的数据,从而形成完整的数据包*/pipeline.addLast("LengthFieldBasedFrameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,8,0,8));// 将上条语句的length加入传递的数据中心pipeline.addLast("LengthFieldPrepender",new LengthFieldPrepender(8));// 传递字符串的编码解码器pipeline.addLast("StringDecoder",new StringDecoder(CharsetUtil.UTF_8));pipeline.addLast("StringEecoder",new StringEncoder(CharsetUtil.UTF_8));// 增加自定义处理器MyNettyClientHandlerpipeline.addLast("MyNettyClientHandler",new MyNettyClientHandler());}
}

3、自定义处理器

package com.guor.demo.netty.chat;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;import java.util.Scanner;/*** 自定义处理器*/
public class MyNettyClientHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String receiveMsg) throws Exception {System.out.println("客户端接收的请求来自:"+ctx.channel().remoteAddress()+",消息内容:"+receiveMsg);System.out.println("请向服务端发送一条消息:");String sendMsg = new Scanner(System.in).nextLine();ctx.channel().writeAndFlush(sendMsg);}public void channelActive(ChannelHandlerContext ctx) throws Exception{ctx.writeAndFlush("第一条消息...");}
}

三、启动服务端、客户端

1、服务端:你好,我是服务端,哪吒编程

在这里插入图片描述

2、客户端:我去,还真连上了,第一次使用Netty通话,真神奇

3、服务端:土包子

在这里插入图片描述


Java高并发编程实战系列文章

Java高并发编程实战1,那些年学过的锁

Java高并发编程实战2,原子性、可见性、有序性,傻傻分不清

Java高并发编程实战3,Java内存模型与Java对象结构

Java高并发编程实战4,synchronized与Lock底层原理

Java高并发编程实战5,异步注解@Async自定义线程池

哪吒精品系列文章

Java学习路线总结,搬砖工逆袭Java架构师

10万字208道Java经典面试题总结(附答案)

SQL性能优化的21个小技巧

Java基础教程系列

Spring Boot 进阶实战
在这里插入图片描述

http://www.lbrq.cn/news/2364643.html

相关文章:

  • 个人做网站怎么盈利推广普通话的文字内容
  • ...温岭做网站网络推广的话术怎么说
  • 网站后台管理系统软件百度服务商
  • 珠宝首饰网站源码最新的域名网站
  • 蒙城做网站西点培训学校
  • 常州建设网站最新国际足球世界排名
  • 美食个人网站设计作品关键词首页排名代做
  • 哪个网站上网好友情链接样式
  • mac和windows做网站浙江seo外包费用
  • 黄埔网站建设公司上海好的网络推广公司
  • 杭州建设企业网站的淘宝seo对什么内容优化
  • 服务器搭建网站方案500字湖南seo服务
  • 如何用wd做网站设计资源网站优化排名软件
  • 网络科技公司网站seo是谁
  • 大米网站模板域名大全
  • 电子商务目前就业形势优化网站技术
  • 房产信息网显示已备案是什么意思广州百度seo优化排名
  • 亚马逊海外版网站新媒体营销推广公司
  • 心理网站开发背景百度竞价点击软件
  • ip达1万的网站怎么做腾讯企点qq
  • asp.net做网站实例关键词优化的五个步骤
  • 成都前十名传媒网站建设网络平台推广方案
  • 给公司做一个网站流程软件培训班学费多少
  • 美工网站做兼职南京seo整站优化技术
  • 网站最近收录怎么开一个网站平台
  • 100款免费软件网站大全兰州seo公司
  • 做网站学的是代码吗seo快速排名软件品牌
  • 购物网站设计理念买卖交易网
  • 安庆市城乡建设网站爱战网关键词挖掘查询工具
  • 全国二级建造师查询网站制作网站需要什么技术
  • 板凳-------Mysql cookbook学习 (十一--------12)
  • 前端-列表fixed冻结的列 横向滚动条拖不动
  • 从零开发足球比分APP:REST API与WebSocket的完美搭配
  • rocky8 --Elasticsearch+Logstash+Filebeat+Kibana部署【7.1.1版本】
  • 网络爬虫的相关知识和操作
  • ESP32S3+VSCode+PlatformIO+Arduino+Freertos开发入门指南:基于Arduino框架的应用开发全流程