快速入门实例-HTTP服务

49 篇文章 0 订阅
25 篇文章 0 订阅

快速入门实例-HTTP服务

实例要求

1.使用IDEA 创建Netty项目

2.Netty 服务器在 9999端口监听,浏览器发出请求 "http://localhost:9999/ "

3.服务器可以回复消息给客户端 "你好,我是服务器 "

代码示例

package com.wxit.http;

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;

/**
 * @Author wj
 **/
public class TestServer {
    public static void main(String[] args) {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new TestServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(9999).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

package com.wxit.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;


/**
 * @Author wj
 **/
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        //向管道加入处理器
        //得到管道
        ChannelPipeline pipeline = ch.pipeline();

        //加入一个netty提供的httpServerCode
        //HttpServerCodec 是netty提供的处理http的编-解码器
        pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
        //增加一个自定义的handler
        pipeline.addLast("MyTestHttpServerHandler",new TestHttpServerHandler());
    }
}

package com.wxit.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * @Author wj
 * HttpObject 客户端和服务器端相互通讯的数据被封装成HttpObject
 **/
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    //channelRead0 读取客户端数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        //判断msg 是不是http request请求
        if (msg instanceof HttpRequest){
            System.out.println("msg类型=" + msg.getClass());
            System.out.println("客户端地址" + ctx.channel().remoteAddress());

            //回复信息给浏览器(http协议)
            ByteBuf content = Unpooled.copiedBuffer("你好,我是服务器", CharsetUtil.UTF_8);

            //构造一个http的响应
            DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);

            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());

            //将构建好response返回
            ctx.writeAndFlush(response);
        }
    }
}

运行结果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
基于VHDL的FPGA开发是一种重要的数字电路设计方法,可以用于实现各种电子设备和系统。下面我将介绍一些快速入门的技巧和实例。 首先,了解VHDL语言和FPGA开发工具是非常重要的。VHDL是一种硬件描述语言,可以用于描述数字电路的行为和结构。FPGA(Field-Programmable Gate Array)是一种可编程逻辑器件,可以根据用户的需求重新配置内部的逻辑单元和连接。常用的开发工具包括Xilinx ISE、Altera Quartus等。熟悉这些工具和语言是FPGA开发的基础。 其次,掌握模块化设计是FPGA开发的关键。VHDL语言支持模块化设计,可以将整个系统划分为多个模块,每个模块负责不同的功能。通过模块化设计,可以简化系统的复杂度,提高开发效率。 接下来,我将以一个简单的数字电路为例来演示FPGA开发的快速入门。假设我们需要设计一个4位全加器,可以通过VHDL语言描述每个加法器的行为和结构,然后使用FPGA开发工具将其综合为目标设备的配置文件。最后,将配置文件下载到FPGA设备中,即可实现4位全加器的功能。这个例子可以帮助初学者快速入门FPGA开发的流程和技巧。 总之,基于VHDL的FPGA开发是一项具有挑战性但也非常有创造性的工作。通过掌握关键的技巧和实例,可以帮助初学者快速入门并在实践中不断提高。希望这些信息对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让美好继续发生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值