netty4.0.23 初学的demo

 

例子共4个文件,用到的jar包有:

netty-all-4.0.23.Final.jar

log4j.jar (apache的)

commons-logging-1.1.1.jar(apache的)

 

文件 TcpServerHandler

 


1. package
2.
3. import
4.
5. import
6. import
7.
8. public class TcpServerHandler extends
9.
10. private static final Logger logger = Logger.getLogger(TcpServerHandler.class);
11.
12. @Override
13. protected void
14. throws
15. "SERVER接收到消息:"+msg);
16. "yes, server is accepted you ,nice !"+msg);
17. }
18.
19. @Override
20. public void
21. throws
22. "Unexpected exception from downstream.", cause);
23. ctx.close();
24. }
25.
26. }

 

 

文件 TcpServer


1. package
2.
3. import
4.
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17.
18. public class
19.
20. private static final Logger logger = Logger.getLogger(TcpServer.class);
21. private static final String IP = "127.0.0.1";
22. private static final int PORT = 9999;
23. /**用于分配处理业务线程的线程组个数 */
24. protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors()*2; //默认
25. /** 业务出现线程大小*/
26. protected static final int BIZTHREADSIZE = 4;
27. /*
28. * NioEventLoopGroup实际上就是个线程池,
29. * NioEventLoopGroup在后台启动了n个NioEventLoop来处理Channel事件,
30. * 每一个NioEventLoop负责处理m个Channel,
31. * NioEventLoopGroup从NioEventLoop数组里挨个取出NioEventLoop来处理Channel
32. */
33. private static final EventLoopGroup bossGroup = new
34. private static final EventLoopGroup workerGroup = new
35.
36. protected static void run() throws
37. new
38. b.group(bossGroup, workerGroup);
39. class);
40. new
41. @Override
42. public void initChannel(SocketChannel ch) throws
43. ChannelPipeline pipeline = ch.pipeline();
44. "frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
45. "frameEncoder", new LengthFieldPrepender(4));
46. "decoder", new
47. "encoder", new
48. new
49. }
50. });
51.
52. b.bind(IP, PORT).sync();
53. "TCP服务器已启动");
54. }
55.
56. protected static void
57. workerGroup.shutdownGracefully();
58. bossGroup.shutdownGracefully();
59. }
60.
61. public static void main(String[] args) throws
62. "开始启动TCP服务器...");
63. TcpServer.run();
64. // TcpServer.shutdown();
65. }
66. }

 

文件 TcpClientHandler


1. package
2.
3. import
4.
5. import
6. import
7.
8. public class TcpClientHandler extends
9.
10. private static final Logger logger = Logger.getLogger(TcpClientHandler.class);
11.
12. @Override
13. protected void
14. throws
15. //messageReceived方法,名称很别扭,像是一个内部方法.
16. "clien

t接收到服务器返回的消息:"+msg);  

  1.           
  2.     }  
  3.   
  4. }  


 

文件 TcpClient


1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16.
17. import
18.
19. public class
20. private static final Logger logger = Logger.getLogger(TcpClient.class);
21. public static String HOST = "127.0.0.1";
22. public static int PORT = 9999;
23.
24. public static
25. public static
26. /**
27. * 初始化Bootstrap
28. * @return
29. */
30. public static final
31. new
32. new
33. class);
34. new
35. @Override
36. protected void initChannel(Channel ch) throws
37. ChannelPipeline pipeline = ch.pipeline();
38. "frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
39. "frameEncoder", new LengthFieldPrepender(4));
40. "decoder", new
41. "encoder", new
42. "handler", new
43. }
44. });
45. true);
46. return
47. }
48.
49. public static final Channel getChannel(String host,int
50. null;
51. try
52. channel = bootstrap.connect(host, port).sync().channel();
53. catch
54. "连接Server(IP[%s],PORT[%s])失败", host,port),e);
55. return null;
56. }
57. return
58. }
59.
60. public static void sendMsg(String msg) throws
61. if(channel!=null){
62. channel.writeAndFlush(msg).sync();
63. else{
64. "消息发送失败,连接尚未建立!");
65. }
66. }
67.
68. public static void main(String[] args) throws
69. try
70. long
71. for (int i = 0; i < 100000; i++) {
72. "你好1");
73. }
74. long
75. 1000000.0);
76. catch
77. e.printStackTrace();
78. }
79. }
80. }

 

 


netty4.0.23 初学的demo

 

例子共4个文件,用到的jar包有:

netty-all-4.0.23.Final.jar

log4j.jar (apache的)

commons-logging-1.1.1.jar(apache的)

 

文件 TcpServerHandler

 



1. package
2.
3. import
4.
5. import
6. import
7.
8. public class TcpServerHandler extends
9.
10. private static final Logger logger = Logger.getLogger(TcpServerHandler.class);
11.
12. @Override
13. protected void
14. throws
15. "SERVER接收到消息:"+msg);
16. "yes, server is accepted you ,nice !"+msg);
17. }
18.
19. @Override
20. public void
21. throws
22. "Unexpected exception from downstream.", cause);
23. ctx.close();
24. }
25.
26. }


 

 

文件 TcpServer



1. package
2.
3. import
4.
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17.
18. public class
19.
20. private static final Logger logger = Logger.getLogger(TcpServer.class);
21. private static final String IP = "127.0.0.1";
22. private static final int PORT = 9999;
23. /**用于分配处理业务线程的线程组个数 */
24. protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors()*2; //默认
25. /** 业务出现线程大小*/
26. protected static final int BIZTHREADSIZE = 4;
27. /*
28. * NioEventLoopGroup实际上就是个线程池,
29. * NioEventLoopGroup在后台启动了n个NioEventLoop来处理Channel事件,
30. * 每一个NioEventLoop负责处理m个Channel,
31. * NioEventLoopGroup从NioEventLoop数组里挨个取出NioEventLoop来处理Channel
32. */
33. private static final EventLoopGroup bossGroup = new
34. private static final EventLoopGroup workerGroup = new
35.
36. protected static void run() throws
37. new
38. b.group(bossGroup, workerGroup);
39. class);
40. new
41. @Override
42. public void initChannel(SocketChannel ch) throws
43. ChannelPipeline pipeline = ch.pipeline();
44. "frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
45. "frameEncoder", new LengthFieldPrepender(4));
46. "decoder", new
47. "encoder", new
48. new
49. }
50. });
51.
52. b.bind(IP, PORT).sync();
53. "TCP服务器已启动");
54. }
55.
56. protected static void
57. workerGroup.shutdownGracefully();
58. bossGroup.shutdownGracefully();
59. }
60.
61. public static void main(String[] args) throws
62. "开始启动TCP服务器...");
63. TcpServer.run();
64. // TcpServer.shutdown();
65. }
66. }



 

文件 TcpClientHandler




1. package
2.
3. import
4.
5. import
6. import
7.
8. public class TcpClientHandler extends
9.
10. private static final Logger logger = Logger.getLogger(TcpClientHandler.class);
11.
12. @Override
13. protected void
14. throws
15. //messageReceived方法,名称很别扭,像是一个内部方法.
16. "client接收到服务器返回的消息:"+msg);
17.
18. }
19.
20. }


 

文件 TcpClient



1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16.
17. import
18.
19. public class
20. private static final Logger logger = Logger.getLogger(TcpClient.class);
21. public static String HOST = "127.0.0.1";
22. public static int PORT = 9999;
23.
24. public static
25. public static
26. /**
27. * 初始化Bootstrap
28. * @return
29. */
30. public static final
31. new
32. new
33. class);
34. new
35. @Override
36. protected void initChannel(Channel ch) throws
37. ChannelPipeline pipeline = ch.pipeline();
38. "frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
39. "frameEncoder", new LengthFieldPrepender(4));
40. "decoder", new
41. "encoder", new
42. "handler", new
43. }
44. });
45. true);
46. return
47. }
48.
49. public static final Channel getChannel(String host,int
50. null;
51. try
52. channel = bootstrap.connect(host, port).sync().channel();
53. catch
54. "连接Server(IP[%s],PORT[%s])失败", host,port),e);
55. return null;
56. }
57. return
58. }
59.
60. public static void sendMsg(String msg) throws
61. if(channel!=null){
62. channel.writeAndFlush(msg).sync();
63. else{
64. "消息发送失败,连接尚未建立!");
65. }
66. }
67.
68. public static void main(String[] args) throws
69. try
70. long
71. for (int i = 0; i < 100000; i++) {
72. "你好1");
73. }
74. long
75. 1000000.0);
76. catch
77. e.printStackTrace();
78. }
79. }
80. }