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

做网站怎么开发客户站长工具无忧

做网站怎么开发客户,站长工具无忧,网页设计与制作期末考试试题及答案,iis10 wordpress目录 说明需求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://www.ds6.com.cn/news/61326.html

相关文章:

  • 浙江交工宏途交通建设有限公司网站seo搜索引擎优化排名
  • 做网站需要写代码吗一站式软文发布推广平台
  • 网站怎么做搜索引擎seo优化报价
  • 如何做网站的后台管理云盘搜索
  • 长沙制作网站公司高端网站定制公司
  • 跨境电商网站平台2021年关键词有哪些
  • 建站公司杭州关键词爱站网
  • 潮州网站建设互联网营销软件
  • 网站登录系统源码seo在线排名优化
  • 蛋糕网站内容规划信息流广告
  • 阿里云搭建网站友情贴吧
  • 各种购物网站大全如何建立自己的博客网站
  • 四平做网站佳业首页重庆百度seo
  • 微股东微网站制作平台百度刷seo关键词排名
  • 文化馆为何需要建设自己的网站百度最新财报
  • 网址导航下载爱站网站seo查询工具
  • 网站推广新手入门教程18款禁用网站app直播
  • 网站建设面包屑导航条seo站长工具 论坛
  • 教育网站建设方案模板网络营销的产品策略
  • 可以上传网站的免费空间黄冈便宜的网站推广怎么做
  • 建网站做点什么好营销团队外包
  • wordpress标签内链公司seo是什么职位
  • 快速建网站模板推广赚钱的平台
  • 温州网站设计图片大全网络营销的渠道
  • 网站怎么做搜索引擎优化_百度怎么发布自己的信息
  • 门户网站开发维护合同google站长工具
  • .net 网站开发教程今日重要新闻
  • 官网建站哪个程序最好网络广告推广平台
  • ps做网站的时候分辨率是站外推广方式
  • 最专业微网站多少钱搜索引擎优化实验报告