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

网站开发流程荆州wordpress主题极简

网站开发流程荆州,wordpress主题极简,亚马逊免费的关键词工具,秀屿区建设局网站目录 一、实现ChannelHandler 二、实现ChannelInitializer 三、实现服务器启动程序 四、测试 本文来实现一个简单的Web服务器#xff0c;当用户在浏览器访问Web服务器时#xff0c;可以返回响应的内容给用户。很简单#xff0c;就三步。 一、实现ChannelHandler pack…目录 一、实现ChannelHandler  二、实现ChannelInitializer 三、实现服务器启动程序 四、测试 本文来实现一个简单的Web服务器当用户在浏览器访问Web服务器时可以返回响应的内容给用户。很简单就三步。 一、实现ChannelHandler  package cn.md.netty.httpserver;import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.*; import io.netty.util.CharsetUtil;/*** * Author: Martin* * Date 2024/9/1 17:47* * Description**/ public class HttpServerHandler extends SimpleChannelInboundHandlerFullHttpRequest {/*** Is called for each message of type {link I}.** param ctx the {link ChannelHandlerContext} which this {link SimpleChannelInboundHandler}* belongs to* param msg the message to handle* throws Exception is thrown if an error occurred*/Overrideprotected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {// 打印Http请求printHttpRequest(msg);String uri msg.uri();String resp;switch (uri) {case /:resp hello world;break;case /test:resp test;break;case /hi:resp hello;break;default:resp 404;}// 返回http格式响应returnHttpResp(ctx, resp);}private void returnHttpResp(ChannelHandlerContext ctx, String msg) {FullHttpResponse response new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));response.headers().set(HttpHeaderNames.CONTENT_LENGTH,msg.length());ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);}private void printHttpRequest(FullHttpRequest msg) {String uri msg.uri();HttpMethod method msg.method();HttpVersion httpVersion msg.protocolVersion();// 打印请求行System.out.println(uri: uri method: method httpVersion: httpVersion);HttpHeaders headers msg.headers();for (String name : headers.names()) {System.out.println(name : headers.get(name));}System.out.println();System.out.println(msg.content().toString(CharsetUtil.UTF_8));} }二、实现ChannelInitializer package cn.md.netty.httpserver;import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec;/*** * Author: Martin* * Date 2024/9/1 17:55* * Description**/ public class HttpServerChannelInitializer extends ChannelInitializerSocketChannel {/*** This method will be called once the {link Channel} was registered. After the method returns this instance* will be removed from the {link ChannelPipeline} of the {link Channel}.** param ch the {link Channel} which was registered.* throws Exception is thrown if an error occurs. In that case it will be handled by* {link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default close* the {link Channel}.*/Overrideprotected void initChannel(SocketChannel ch) throws Exception {// 添加自定义的handlerch.pipeline().addLast(codec,new HttpServerCodec()) // 添加编解码器// 添加聚合器聚合为一个完整的 FullHttpMessage.addLast(aggregator,new HttpObjectAggregator(1024*1024*10)).addLast(handler,new HttpServerHandler());} }三、实现服务器启动程序 package cn.md.netty.httpserver;import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel;/*** * Author: Martin* * Date 2024/9/1 18:01* * Description**/ public class HttpServer {public static void main(String[] args) {NioEventLoopGroup bossGroup new NioEventLoopGroup(1);NioEventLoopGroup workerGroup new NioEventLoopGroup();ServerBootstrap serverBootstrap new ServerBootstrap();try {ChannelFuture channelFuture serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new HttpServerChannelInitializer())//服务器在处理客户端连接请求时的等待队列长度。//当服务器接收到客户端的连接请求时如果服务器正在处理其他连接或者处于忙碌状态新的连接请求将被放入等待队列中。.option(ChannelOption.SO_BACKLOG, 128)//底层套接字级别设置的选项由操作系统的 TCP/IP 协议栈实现保活机制。//当开启后在一定时间没有数据传输时操作系统自动发送保活探测报文来检测连接是否仍然有效。.option(ChannelOption.SO_KEEPALIVE, true).bind(8888).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {throw new RuntimeException(e);} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}}四、测试 我是马丁如果你喜欢麻烦点个赞 下期见
http://www.yingshimen.cn/news/141434/

相关文章:

  • 有个网站可以学做ppt模板营销策划主题
  • 网站低保图用什么做wordpress安装对搜索引擎
  • 做怎样的企业网站北京大学php培训网站源码(dede
  • asp新闻发布网站模板下载宁波网站建设托管
  • 网站流量指标新手学做网站学要做哪些
  • 温州网络公司网站建设有做销售产品的网站
  • 网站建设企业需要符合哪些建win7 iis建立网站
  • 企业网站建设案例有哪些公司建立生育支持政策体系
  • 网站项目开发流程有哪七步建c2c网站
  • 医院做网站运维我的建筑网
  • 北京美的网站网站内容更新用什么
  • 租房网站的财务分析表怎么做辽宁沈阳建设工程信息网站
  • 和动物做的网站吗想学网站建设方向的研究生
  • 网站策划是做什么的免费咨询服务期
  • 网站建设教学改进wordpress简体中文版
  • 某班级网站建设方案论文wordpress流动图片
  • 通化公司做网站网站功能需求怎么写
  • 建设网站群的指导思想网站框架与内容
  • 温州百度搜索网站排名wordpress添加页面模块
  • 文化厅加强网站建设自助建设网站软件
  • 做网站注册页面WordPress会员月卡年卡
  • thinkphp大型网站开发金蝶财务软件官网报价
  • 网站自行备案wordpress互动插件
  • html公益网站模板wordpress 调用文章图片
  • 哪里有做配音的兼职网站品牌战略
  • 北京赛车网站建设北理离线《网站开发与应用》
  • 建网站张掖哪家强?炫酷的网站
  • 湘潭做网站 磐石网络很专业百度推广点击收费标准
  • 重庆建设网站公司智慧团建网页电脑版登录网站
  • 网站价值如何评估旅游宣传网站建设方案