netty http server入门练习
来源:互联网 发布:哇掌柜是什么软件 编辑:程序博客网 时间:2024/04/29 01:44
使用netty写一个简单的http server
package org.game.throne.proxy;import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.ByteBufProcessor;import io.netty.buffer.Unpooled;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.http.*;import io.netty.util.CharsetUtil;import java.util.ArrayList;import java.util.List;/** * Created by lvtu on 2017/8/31. */class MServerHandler extends ChannelInboundHandlerAdapter { private StringBuilder body = new StringBuilder(); private List<Byte> bb = new ArrayList<>(); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if(msg instanceof DefaultHttpContent){ DefaultHttpContent httpContent = (DefaultHttpContent)msg; ByteBuf content = httpContent.content(); content.forEachByte(new ByteBufProcessor(){ @Override public boolean process(byte value) throws Exception { bb.add(value); return true; } }); body.append(content.toString()); } if(msg instanceof DefaultLastHttpContent){ byte[] b = new byte[bb.size()]; for(int i=0;i<bb.size();i++){ b[i] = bb.get(i); } System.out.println(new String(b,"utf-8")); ctx.flush(); } if (msg instanceof HttpRequest) { ByteBuf content = Unpooled.copiedBuffer("Hello World.", CharsetUtil.UTF_8); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");//必须加这个,否则浏览器不响应 response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());//必须加这个,否则浏览器不响应 response.headers().set(HttpHeaders.Names.CONNECTION, "keep-alive"); ctx.write(response); } } @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); }}public class MServer { private int port; public MServer(int port) { this.port = port; } public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) // (3) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new MServerHandler()); } }) .option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); // (7) // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } public static void main(String[] args) { try { new MServer(8082).run(); }catch (Exception e){ e.printStackTrace(); } }}
问题点
1.netty通过文档或者看源码,都可以达到简单使用
2.最大的问题点是上面代码标注红色的地方,如果没有正确设定http response,浏览器是不会相应的,这是http协议规范的吗?
阅读全文
0 0
- netty http server入门练习
- Netty Http server & Client
- 【Netty入门】基于Netty的Server / Client
- Netty入门之-http文件服务器
- Practical Netty (6) HTTP Server/Client
- Netty-Http
- netty http
- 【Netty入门】Netty概述
- Netty Server
- 【Netty基础】基于HTTP的文件下载Server实例
- 【Netty基础】基于HTTP的上传Server实例
- Netty 入门
- Netty入门
- Netty入门
- netty入门
- Netty入门
- Netty入门
- Netty入门
- OkHttp封装之后使用
- HDU 4857 逃生 (逆向拓扑排序、优先队列)
- python安装详解
- FORM表单序列化
- 为什么jsp页面使用javascript代码没用,连alert也没有y
- netty http server入门练习
- HDU 6183 && 2017广西邀请赛:Color it(线段树)
- 将表单序列化为json对象
- php实现阿里云签名类
- 关于含有对象成员的类如何进行初始化?
- 统计单词个数的简单程序
- Java编程:浅析 HashMap 中数组的 size 为什么必须是 2 的整数次幂
- iOS开发稀碎小技巧一
- 数据库拆分:横向拆分和纵向拆分