Netty复习:(1)Http server: hello world

一、加依赖

复制代码
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.72.Final</version>
        </dependency>

二、创建自定义的handler

复制代码
package cn.edu.tju.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;

import java.nio.ByteBuffer;

public class MyHttpHandler extends SimpleChannelInboundHandler<HttpObject> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject httpObject) throws Exception {
        System.out.println("get info from client...");
        String message = "hello, world";
        ByteBuf byteBuf = Unpooled.copiedBuffer(message.getBytes());
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK, byteBuf);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, message.length() );
        ctx.channel().writeAndFlush(response);
    }
}

三、定义启动类,在pipeline中添加自定义的handler

复制代码
package cn.edu.tju;


import cn.edu.tju.handler.MyHttpHandler;
import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

public class NettyHttpServer {
    private static int port = 9093;
    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 ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            channel.pipeline().addLast("httpServerCodec", new HttpServerCodec());
                            channel.pipeline().addLast("myHttpHandler", new MyHttpHandler());
                        }
                    });
            ChannelFuture channelFuture=serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();




        }catch (Exception ex){

        }

    }
}
相关推荐
24zhgjx-lxq16 分钟前
华为ensp:MSTP
网络·安全·华为·hcip·ensp
ling___xi19 分钟前
《计算机网络》计网3小时期末速成课各版本教程都可用谢稀仁湖科大版都可用_哔哩哔哩_bilibili(笔记)
网络·笔记·计算机网络
REDcker29 分钟前
Linux 文件描述符与 Socket 选项操作详解
linux·运维·网络
Up九五小庞1 小时前
用arpspoof实现100%批量切断192.168.110.10 - 192.168.110.100 断网(双向欺骗)--九五小庞
网络·开源
躺柒1 小时前
读数字时代的网络风险管理:策略、计划与执行04风险指引体系
大数据·网络·信息安全·数字化·网络管理·网络风险管理
独角鲸网络安全实验室1 小时前
本地信任成“致命漏洞”:数千Clawdbot Agent公网裸奔,供应链与内网安全告急
网络·网关·安全·php·漏洞·clawdbot·信任机制漏洞
ai_xiaogui2 小时前
Tailscale实现家庭与公司网络双向通信教程:子网路由配置详解
网络·tailscale·双向通信·子网路由配置详解·tailscale双向互访
mftang2 小时前
现代互联网安全的基石: SSL/TLS技术介绍
服务器·网络·ssl
坐怀不乱杯魂2 小时前
Linux网络 - 网络层
linux·服务器·网络
Teame_2 小时前
渗透课程学习总结
网络·安全·web安全