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){

        }

    }
}
相关推荐
东北甜妹38 分钟前
TCP/IP和VLAN
网络协议·tcp/ip·面试
SPC的存折40 分钟前
Cisco Packet Tracer 8.0 上的 VLAN 综合实验报告
运维·网络
yuezhilangniao44 分钟前
告别网络排障恐惧症-告别UI版wireshark:用 curl + tcpdump + tshark + ss 构建完整工具链 - 含TCPIP老鸟常识
网络·wireshark·tcpip
你觉得脆皮鸡好吃吗1 小时前
SQL注入 手工注入
网络·数据库·sql·安全·web安全·网络安全学习
盟接之桥1 小时前
盟接之桥®制造业EDI软件:专注制造,为制造业服务,让全球供应链协同更有底气
网络·安全·低代码·汽车·制造
eam0511231 小时前
简单园区网
网络
deepdata_cn1 小时前
开放智能体网络协议(Agent Network Protocol,ANP)
网络协议·智能体
Cat_Rocky1 小时前
网络技术基础一点点
运维·服务器·网络
Lyyaoo.1 小时前
【JAVA网络面经】应用层协议
java·开发语言·网络
@insist1231 小时前
网络工程师-非网络核心知识操作系统与系统开发基础
网络·网络工程师·软考·软件水平考试