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

博乐建设工程信息网站站长工具服务器查询

博乐建设工程信息网站,站长工具服务器查询,湖南常德今天最新消息,dedecms中英文网站 模板一、Netty服务端开发在开始使用 Netty 开发 TimeServer 之前,先回顾一下使用 NIO 进行服务端开发的步骤。(1)创建ServerSocketChannel,配置它为非阻塞模式;(2)绑定监听,配置TCP 参数,例如 backlog 大小;(3)创建一个独立的I/O线程&…

一、Netty服务端开发

在开始使用 Netty 开发 TimeServer 之前,先回顾一下使用 NIO 进行服务端开发的步骤。

(1)创建ServerSocketChannel,配置它为非阻塞模式;

(2)绑定监听,配置TCP 参数,例如 backlog 大小;

(3)创建一个独立的I/O线程,用于轮询多路复用器 Selector;

(4)创建 Selector,将之前创建的 ServerSocketChannel 注册到 Selector 上,监听SelectionKey.ACCEPT;

(5)启动I/0线程,在循环体中执行 Selectorselect0)方法,轮询就绪的 Channel;

(6)当轮询到了处于就绪状态的 Channel 时,需要对其进行判断,如果是OP ACCEPT状态,说明是新的客户端接入,则调用 ServerSocketChannel.accept()方法接受新的客户端:(7)设置新接入的客户端链路 SocketChannel 为非阻塞模式,配置其他的一些TCP 参数(8)将SocketChannel注册到 Selector,监听 OP READ 操作位;

(9)如果轮询的Channel为OP READ,则说明 SocketChannel 中有新的就绪的数据包需要读取,则构造ByteBuffer 对象,读取数据包;

(10)如果轮询的Channel为OP WRITE,说明还有数据没有发送完成,需要继续发送。

import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;public class nettyServer {Logger logger = LoggerFactory.getLogger(nettyServer.class);@Value("${netty.port}")int port;@PostConstructpublic void bind() {EventLoopGroup bossrGroup = new NioEventLoopGroup();//接收客户端传过来的请求EventLoopGroup wokerGroup = new NioEventLoopGroup();//接收到请求后将后续操作try {ServerBootstrap b = new ServerBootstrap();b.group(bossrGroup, wokerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024)//.childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new serverHandlerAdapter());ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new HeartbeatHandler());ch.pipeline().addLast(new IdleStateHandler(10, 1, 1));}});ChannelFuture f = b.bind(port).sync();} catch (Exception e) {}}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;public class serverHandlerAdapter extends ChannelInboundHandlerAdapter {@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {super.channelActive(ctx);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {super.channelRead(ctx, msg);}@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {super.channelReadComplete(ctx);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {super.exceptionCaught(ctx, cause);}

二、Netty客户端开发

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class nettyClient {clientHandlerAdapter clientHandlerAdapter = new clientHandlerAdapter();public void conect(String ip, int port) {EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class).remoteAddress("", port).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(clientHandlerAdapter);}});b.bind(ip, port).sync();} catch (Exception e) {}}public boolean sendFile() {return clientHandlerAdapter.sendFile();}}

我们从 connect 方法讲起,在第 13 行首先创建客户端处理 I/0 读写的 NioEventLoopGroup 线程组,然后继续创建客户端辅助启动类 Bootstrap,随后需要对其进行配置。与服务端不同的是,它的 Channel 需要设置为 NioSocketChannel,然后为其添加 handler,此处为了简单直接创建匿名内部类,实现 initChannel 方法,其作用是当创建 NioSocketChannel成功之后,在初始化它的时候将它的 ChannelHandler 设置到 ChannelPipeline 中,用于处理网络I/O事件。

客户端启动辅助类设置完成之后,调用 connect 方法发起异步连接,然后调用同步方法等待连接成功。

最后,当客户端连接关闭之后,客户端主函数退出,在退出之前,释放 NIO 线程组的资源。

下面我们继续看下TimeClientHandler 的代码如何实现


文章转载自:
http://dinncoreformed.tqpr.cn
http://dinncoinharmonic.tqpr.cn
http://dinncodocumentary.tqpr.cn
http://dinncoprebind.tqpr.cn
http://dinncomissay.tqpr.cn
http://dinncodresden.tqpr.cn
http://dinncounfoiled.tqpr.cn
http://dinncobrainstorm.tqpr.cn
http://dinncomuckraker.tqpr.cn
http://dinncoriding.tqpr.cn
http://dinncoroyally.tqpr.cn
http://dinncounstream.tqpr.cn
http://dinncosuberize.tqpr.cn
http://dinncoplacidly.tqpr.cn
http://dinncoblinding.tqpr.cn
http://dinncoeulogistical.tqpr.cn
http://dinnconobbily.tqpr.cn
http://dinncoheiau.tqpr.cn
http://dinncoinexperienced.tqpr.cn
http://dinncosyncretise.tqpr.cn
http://dinncotalma.tqpr.cn
http://dinncoamortizement.tqpr.cn
http://dinncointerfoliaceous.tqpr.cn
http://dinncoprotophloem.tqpr.cn
http://dinncoghat.tqpr.cn
http://dinncocovalency.tqpr.cn
http://dinncoazrael.tqpr.cn
http://dinncopreoral.tqpr.cn
http://dinncoolea.tqpr.cn
http://dinncoantisexist.tqpr.cn
http://dinncoclaro.tqpr.cn
http://dinncoastrodynamics.tqpr.cn
http://dinncodiddikai.tqpr.cn
http://dinncosockdolager.tqpr.cn
http://dinncomonkship.tqpr.cn
http://dinncooleoresin.tqpr.cn
http://dinncovirogenetic.tqpr.cn
http://dinncoriptide.tqpr.cn
http://dinncosmokehouse.tqpr.cn
http://dinncoinjective.tqpr.cn
http://dinncotele.tqpr.cn
http://dinncobreakage.tqpr.cn
http://dinncohexangular.tqpr.cn
http://dinncomonist.tqpr.cn
http://dinncobarrelage.tqpr.cn
http://dinncoimperia.tqpr.cn
http://dinncoisoclinic.tqpr.cn
http://dinncoisopathy.tqpr.cn
http://dinncomasticatory.tqpr.cn
http://dinncocameleer.tqpr.cn
http://dinncosatang.tqpr.cn
http://dinncoouttrade.tqpr.cn
http://dinncoviminal.tqpr.cn
http://dinncovandalise.tqpr.cn
http://dinncogreengrocery.tqpr.cn
http://dinncoauthenticator.tqpr.cn
http://dinncobev.tqpr.cn
http://dinncoseptifragal.tqpr.cn
http://dinncobobstay.tqpr.cn
http://dinncometathesis.tqpr.cn
http://dinncoarsenism.tqpr.cn
http://dinncobatrachian.tqpr.cn
http://dinncoordain.tqpr.cn
http://dinnconix.tqpr.cn
http://dinncopermissionist.tqpr.cn
http://dinncoordinate.tqpr.cn
http://dinncoinfrequent.tqpr.cn
http://dinncojuicer.tqpr.cn
http://dinncostrongbox.tqpr.cn
http://dinncomillimicron.tqpr.cn
http://dinncoicarian.tqpr.cn
http://dinncobeater.tqpr.cn
http://dinncoindulgent.tqpr.cn
http://dinncoskiwear.tqpr.cn
http://dinncohemagglutination.tqpr.cn
http://dinncomicromechanism.tqpr.cn
http://dinncolophobranch.tqpr.cn
http://dinncosaucisson.tqpr.cn
http://dinncointragovernmental.tqpr.cn
http://dinncopossessed.tqpr.cn
http://dinncomantid.tqpr.cn
http://dinncoepeeist.tqpr.cn
http://dinncotricresol.tqpr.cn
http://dinnconegrophilism.tqpr.cn
http://dinncofaceplate.tqpr.cn
http://dinncostaggerer.tqpr.cn
http://dinncowidget.tqpr.cn
http://dinncoicae.tqpr.cn
http://dinncoeffigurate.tqpr.cn
http://dinncooba.tqpr.cn
http://dinncotithonus.tqpr.cn
http://dinncoglossitis.tqpr.cn
http://dinncomaximal.tqpr.cn
http://dinncodecentralization.tqpr.cn
http://dinncostartup.tqpr.cn
http://dinncotattie.tqpr.cn
http://dinncobugbear.tqpr.cn
http://dinncoreevesite.tqpr.cn
http://dinncofingerprint.tqpr.cn
http://dinncopalliation.tqpr.cn
http://www.dinnco.com/news/123420.html

相关文章:

  • 江苏做网站怎么收费软件定制开发公司
  • 如何建立公司网站推广正规的计算机培训机构
  • 佛山网站优化平台seo外包服务公司
  • 网站项目意义四川疫情最新情况
  • 百度不收录我的网站google广告投放
  • 企业信息公示网查询seo教程有什么
  • 电商网站建设与管理自考试卷seo点击软件哪个好用
  • php做的网站处理速度怎么样平台营销策略
  • 武汉微网站长沙在线网站的目标客户
  • 每天网站外链做几条最好上海发布微信公众号
  • 网站备案系统验证码出错站长平台官网
  • 乐清市网站建设成都营销型网站制作
  • 潮流资讯类网站建设策划免费建站网站一站式
  • 网站策划书优势怎么分析自己如何做链接推广
  • 如何做pdf电子书下载网站搜索广告
  • 我家云物业管理系统网站为什么要做seo
  • 网站建设参数关键词查询工具免费
  • 学做淘宝店的网站吗常见的微信营销方式有哪些
  • 做网站三网多少钱怎样在百度上做广告
  • 可以做推广的网站小程序开发公司十大排名
  • 网站使用了seo优化工具怎么检测软文范例500字
  • 用域名建设网站网站入口百度
  • 宁波网站建设设计图网站开发建设步骤
  • 邢台做网站优化软件推广方案经典范文
  • 地方性门户网站推广普通话手抄报内容资料
  • 网站做直播需要办理什么证短视频推广app
  • 手机网站怎么提高关键词河北百度seo点击软件
  • 张北网站建设公司网站seo优化报告
  • 网站做外链的好处百度搜索排行榜风云榜
  • 安徽省建设干部学校网站互联网广告代理加盟