---
title: "Netty小demo"
author: "ssxxjjii"
date: "2013-04-27T15:01:06Z"
tags: []
categories: ["互联网"]
slug: "netty-demo"
draft: false
lastmod: "2013-04-27T15:01:06Z"
id: 1855900
url: "https://tdcq.iteye.com/blog/1855900"
---

最近一段时间一直研究聊天程序，学习了nio的知识，以后会和大家分享的，今天写了一个可以运行看到结果的netty helloworld程序，这个程序虽然简单，但从这个程序可以知道netty的运行流程，希望对初学者有所帮助。

第一步：建立一个maven项目，或者读者可以自己导入netty依赖包

Java代码  [![收藏代码](http://xuehanxin.iteye.com/images/icon_star.png)]()

1. &lt;dependency&gt;
2. &lt;groupId&gt;io.netty&lt;/groupId&gt;
3. &lt;artifactId&gt;netty&lt;/artifactId&gt;
4. &lt;version&gt;3.5.6.Final&lt;/version&gt;
5. &lt;/dependency&gt;

 现在netty 4.0已经出来了，因为3.x版本的资料比较多有利于学习，先从3.x开始了，以后会继续出4.x的学习笔记的，呵呵，上程序了

第二步：复制下面代码到项目中

 

Java代码  [![收藏代码](http://xuehanxin.iteye.com/images/icon_star.png)]()

01. package com.my.day1;
02. import java.net.InetSocketAddress;
03. import java.util.concurrent.Executors;
04. import org.jboss.netty.bootstrap.ServerBootstrap;
05. import org.jboss.netty.channel.Channel;
06. import org.jboss.netty.channel.ChannelHandlerContext;
07. import org.jboss.netty.channel.ChannelPipeline;
08. import org.jboss.netty.channel.ChannelStateEvent;
09. import org.jboss.netty.channel.MessageEvent;
10. import org.jboss.netty.channel.SimpleChannelHandler;
11. import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
12. import org.jboss.netty.handler.codec.string.StringDecoder;
13. import org.jboss.netty.handler.codec.string.StringEncoder;
14. /\**
15. * @Title: NettyDemo.java
16. * @Package com.my.day1
17. * @Description: 一个类完整运行netty，运行就能看到结果
18. * @author jimmy lovelyxuehanxin@163.com
19. * @date 2013-4-16 下午9:38:29
20. \*/
21. public class NettyDemo {
22. public static void main(String\[] args) {
23. NioServer server = new NioServer();
24. }
25. }
26. class NioServer{
27. ServerBootstrap bootstrap;
28. Channel parentChannel;
29. InetSocketAddress localAddress;
30. MyChannelHandler channelHandler = new MyChannelHandler();
31. public NioServer(){
32. bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
33. Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
34. ChannelPipeline pineline = bootstrap.getPipeline();
35. // String格式字符串解码写的时候都会经过这个过滤器处理
36. pineline.addLast("encode", new StringEncoder());
37. // 接受信息的时候会被处理
38. pineline.addLast("decode", new StringDecoder());
39. // 自定义处理类，我们的业务一般从这个类开始
40. pineline.addLast("servercnfactory", channelHandler);
41. bootstrap.bind(new InetSocketAddress(8080));
42. }
43. }
44. // 处理channel中的数据,SimpleChannelHandler帮我们实现好了很多有用户的方法这里就只重写了几个方法
45. class MyChannelHandler extends SimpleChannelHandler{
46. // netty默认信息接受入口
47. @Override
48. public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
49. throws Exception {
50. System.out.println("接受到的信息" + e.getMessage());
51. // 返回信息可以在dos对话框中看到自己输的内容
52. e.getChannel().write(e.getMessage());
53. super.messageReceived(ctx, e);
54. }
55. @Override
56. public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
57. throws Exception {
58. System.out.println("channel Connected......");
59. super.channelConnected(ctx, e);
60. }
61. @Override
62. public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
63. throws Exception {
64. System.out.println("channelClosed");
65. super.channelClosed(ctx, e);
66. }
67. }

 第三步：运行上面的代码，cmd打开dos窗口输入telnet 127.0.0.1 8080 输入内容 程序就可以打印你输入的内容并且可以在dos命令框里打印出你输入的内容。

 

总结：这个列子很简单，可以清楚程序怎么运行的，对理解netty很有帮助