Netty笔记一(可以运行看到结果的简单例子)
2017-09-27 00:00
507 查看
最近一段时间一直研究聊天程序,学习了nio的知识,以后会和大家分享的,今天写了一个可以运行看到结果的netty helloworld程序,这个程序虽然简单,但从这个程序可以知道netty的运行流程,希望对初学者有所帮助。
第一步:建立一个maven项目,或者读者可以自己导入netty依赖包
现在netty 4.0已经出来了,因为3.x版本的资料比较多有利于学习,先从3.x开始了,以后会继续出4.x的学习笔记的,呵呵,上程序了
第二步:复制下面代码到项目中
第三步:运行上面的代码,cmd打开dos窗口输入telnet 127.0.0.1 8080 输入内容 程序就可以打印你输入的内容并且可以在dos命令框里打印出你输入的内容。
总结:这个列子很简单,可以清楚程序怎么运行的,对理解netty很有帮助
第一步:建立一个maven项目,或者读者可以自己导入netty依赖包
<dependency> <groupId>io.netty</groupId> <artifactId>netty</artifactId> <version>3.5.6.Final</version> </dependency>
现在netty 4.0已经出来了,因为3.x版本的资料比较多有利于学习,先从3.x开始了,以后会继续出4.x的学习笔记的,呵呵,上程序了
第二步:复制下面代码到项目中
package com.my.day1; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; /** * @Title: NettyDemo.java * @Package com.my.day1 * @Description: 一个类完整运行netty,运行就能看到结果 * @author jimmy lovelyxuehanxin@163.com * @date 2013-4-16 下午9:38:29 */ public class NettyDemo { public static void main(String[] args) { NioServer server = new NioServer(); } } class NioServer{ ServerBootstrap bootstrap; Channel parentChannel; InetSocketAddress localAddress; MyChannelHandler channelHandler = new MyChannelHandler(); public NioServer(){ bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); ChannelPipeline pineline = bootstrap.getPipeline(); // String格式字符串解码写的时候都会经过这个过滤器处理 pineline.addLast("encode", new StringEncoder()); // 接受信息的时候会被处理 pineline.addLast("decode", new StringDecoder()); // 自定义处理类,我们的业务一般从这个类开始 pineline.addLast("servercnfactory", channelHandler); bootstrap.bind(new InetSocketAddress(8080)); } } // 处理channel中的数据,SimpleChannelHandler帮我们实现好了很多有用户的方法这里就只重写了几个方法 class MyChannelHandler extends SimpleChannelHandler{ // netty默认信息接受入口 @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { System.out.println("接受到的信息" + e.getMessage()); // 返回信息可以在dos对话框中看到自己输的内容 e.getChannel().write(e.getMessage()); super.messageReceived(ctx, e); } @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channel Connected......"); super.channelConnected(ctx, e); } @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { System.out.println("channelClosed"); super.channelClosed(ctx, e); } }
第三步:运行上面的代码,cmd打开dos窗口输入telnet 127.0.0.1 8080 输入内容 程序就可以打印你输入的内容并且可以在dos命令框里打印出你输入的内容。
总结:这个列子很简单,可以清楚程序怎么运行的,对理解netty很有帮助
相关文章推荐
- Netty笔记一(可以运行看到结果的简单例子)
- Java动态编译一个简单的例子(我转载的,但是经过修定,可以在Eclipse下运行)
- 简单的说一下:tarits技法就是一种模板元编程,起可以将本来处于运行期的事拉到编译期来做,增加了运行效率。 看以非模板元编程的例子,就是前面的那个例子:
- Android SDK中的API Demos的所有自带例子的运行结果 (1)
- linux下运行caffe自带的两个简单例子(格式转换及训练模型)
- 解析百度搜索结果页面的python脚本(Linux/Win都可以运行)
- php计算程序运行时间的简单例子分享
- 最简单的0-1背包问题c++代码实例及运行结果
- 运行caffe自带的两个简单例子
- vector向量练习小例子c++代码实例及运行结果
- 一个简单的多态小例子 初学者可以看看
- Mule学习(1)——安装,配置,以及简单例子的运行
- HTML网页中插入输入框,输入框内容或代码可点击"运行代码"运行,在浏览器看到运行输入框内代码效果。此HTML代码由表单代码textarea和简单JS代码实现。 HTML代码: <textarea n
- 一个简单的sql的游标处理结果集的例子
- C# Socket简单例子(服务器与客户端通信)和客户端批运行
- Android SDK中的API Demos的所有自带例子的运行结果 (2)
- Caffe(3):运行caffe自带的两个简单例子
- 书上的例子运行不出来,有谁可以解答下阿
- 最简单的0-1背包问题c++代码实例及运行结果
- flask蓝图(Blueprint)简单测试例子运行不成功的原因:注册时机不对