Netty学习(一)使用官网例子快速开发一个服务器
来源:互联网 发布:淘宝无忧退货流程 编辑:程序博客网 时间:2024/06/07 06:28
Netty学习(一)使用官网例子快速开发一个服务器
说明
- 使用netty4.1.13版本
- oracle-jdk-1.8.0_131
代码托管
https://code.csdn.net/u012995856/mynetty/tree/master
代码
DiscardServerHandler
package cn.pangpython.mynetty.main;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.util.ReferenceCountUtil;/** * @Project MyNetty * @Package cn.pangpython.mynetty.main * @Author pangPython * @Time 上午7:42:42 */public class DiscardServerHandler extends ChannelInboundHandlerAdapter{ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf in = (ByteBuf) msg; try { while (in.isReadable()) { // (1) //打印请求信息 System.out.print((char) in.readByte()); System.out.flush(); } } finally { //释放 ReferenceCountUtil.release(msg); // (2) } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // TODO Auto-generated method stub cause.printStackTrace(); ctx.close(); }}
DiscardServer
package cn.pangpython.mynetty.main;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;/** * @Project MyNetty * @Package cn.pangpython.mynetty.main * @Author pangPython * @Time 上午7:49:34 */public class DiscardServer { private int port; public DiscardServer(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 { ch.pipeline().addLast(new DiscardServerHandler()); } }) .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) throws Exception { int port; if (args.length > 0) { port = Integer.parseInt(args[0]); } else { port = 8080; } new DiscardServer(port).run(); }}
访问
用浏览器访问本机8080端口
localhost:8080
访问结果
阅读全文
0 0
- Netty学习(一)使用官网例子快速开发一个服务器
- Netty学习(二)官网例子:时间服务器
- 一个简单的Netty服务器例子
- Netty学习笔记(一)EchoServer服务器
- 使用netty开发透传服务器
- Netty开发的例子
- java netty服务器简单例子
- Netty学习(一)—基本使用
- 【Netty】netty学习笔记一
- Netty自学-Netty学习(一)
- netty在游戏服务器开发中的应用(一)
- 使用Netty构建一个多线程服务器与客户端
- Netty编程框架开发例子
- netty开发一个程序。
- Netty学习笔记一
- netty 学习记录一
- Netty学习(一)
- Netty学习(一)
- ios-Category
- 策略模式
- 获得屏幕高度和虚拟按键高度
- React组件生命周期过程说明
- sublime_text写的框架小结
- Netty学习(一)使用官网例子快速开发一个服务器
- HNOI2011 数学作业
- 写在前面
- 10017---java NIO详解
- OpenCV演示程序 汇总
- 【二维树状数组--模板】poj 2155 Matrix、poj 1195 Mobile phones
- 监听器+过滤器+拦截器区别(二)
- 类方法与实例方法
- Struts2中只对登陆时验证