Server. Httpserver.java
package server;import io.netty.bootstrap.serverbootstrap;import io.netty.channel.channelfuture; import io.netty.channel.channelinitializer;import io.netty.channel.channeloption;import io.netty.channel.eventloopgroup;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.HttpRequestDecoder;import io.netty.handler.codec.http.httpresponseencoder;import org.apache.log4j.logger;public class Httpserver {private logger logger = logger.getlogger (HttpServer.class); public void start (Int port) throws Exception { logger.info ("http server listening on " +port+ " ..."); eventloopgroup Bossgroup = new nioeventloopgroup (); Eventloopgroup workergroup = new nioeventloopgroup (); try { serverbootstrap b = new serverbootstrap (); b.group (Bossgroup, workergroup) .channel (Nioserversocketchannel.class) .childhandler (new channelinitializer<socketchannel> () { @ override public vOid initchannel (socketchannel ch) throws Exception { The // server side sends the HttpResponse, so use Httpresponseencoder to encode Ch.pipeline (). AddLast (New httpresponseencoder ()); // The server side is receiving HttpRequest, so use Httprequestdecoder for decoding ch.pipeline (). AddLast (New httprequestdecoder ()); &Nbsp; ch.pipeline (). AddLast (New httpserverinboundhandler ()); } }). Option (ChannelOption.SO _backlog, 128) . Childoption (channeloption.so_keepalive, true); channelfuture f = b.bind (port). Sync (); f.channel (). Closefuture (). Sync (); } finally { Workergroup.shutdowngracefully (); Bossgroup.shutdowngracefully (); } } public static Void main (String[] args) throws Exception { httpserver server = new httpserver (); server.start (8844); }}
Server. Httpserverinboundhandler.java
package server;import static io.netty.handler.codec.http.httpheaders.names.content_length; import static io.netty.handler.codec.http.httpheaders.names.content_type;import static io.netty.handler.codec.http.httpresponsestatus.ok;import static io.netty.handler.codec.http.httpversion.http_1_1;import java.util.random;import io.netty.buffer.bytebuf;import io.netty.buffer.unpooled;import io.netty.channel.channelhandlercontext;import io.netty.channel.channelinboundhandleradapter;import io.netty.handler.codec.http.defaultfullhttpresponse;import io.netty.handler.codec.http.fullhttpresponse; Import io.netty.handler.codec.http.httpcontent;import io.netty.handler.codec.http.httprequest;import org.apache.log4j.Logger;public class HttpServerInboundHandler extends Channelinboundhandleradapter{private logger logger = logger.getlogger ( Httpserverinboundhandler.class); @Override public void channelread (ChannelHandlerContext &NBSP;CTX,&NBSP;OBJECT&NBSP;MSG) throws Exception { if (msg instanceof httprequest) { HttpRequest request = (HttpRequest) msg; string uri = request.geturi (); logger.info ("Uri:" + uri); } if (msg Instanceof httpcontent) { Httpcontent content =&nBSP; (httpcontent) msg; bytebuf buf = content.content (); system.out.println (buf.tostring (io.netty.util.CharsetUtil.UTF_8)); buf.release (); string res = "the random number from server is : " +new random (). NEXTINT (100); fullhttpresponse response = new defaultfullhttpresponse (Http_1_1,ok, unpooled.wrappedbuffer (Res.getBytes ("UTF-8")); response.headers (). Set (CONTENT_TYPE, " Text/plain "); &Nbsp; response.headers (). Set (CONTENT_LENGTH, Response.content (). Readablebytes ()); Ctx.write (response); ctx.flush (); ctx.close (); } ctx.firechannelread (msg); // jump to the next handler }}
Netty4 Simple HTTP Service setup