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

        }

    }
}
相关推荐
Johny_Zhao4 分钟前
Vmware workstation安装部署微软SCCM服务系统
网络·人工智能·python·sql·网络安全·信息安全·微软·云计算·shell·系统运维·sccm
会员果汁6 分钟前
网络实验-VRRP
网络
XiaoCCCcCCccCcccC20 分钟前
Linux 的 UDP 网络编程 -- 回显服务器,翻译服务器
linux·网络·udp
獨枭35 分钟前
Visual Studio 2022 跨网络远程调试
网络·ide·visual studio
深度学习040735 分钟前
【网络实验】-BGP-EBGP的基本配置
网络·智能路由器
阿斯顿法国红酒快1 小时前
Windows系统安全加固
网络·windows·安全·网络安全·系统安全·ddos
sun0077002 小时前
windows 10 做服务器 其他电脑无法访问,怎么回事?
运维·服务器·网络
2401_896008193 小时前
TCP连接状态说明
运维·服务器·网络
利刃大大3 小时前
【网络编程】十、详解 UDP 协议
网络·网络协议·udp
LaoZhangGong1233 小时前
W5500使用ioLibrary库创建TCP客户端
网络·经验分享·stm32·网络协议·tcp/ip