Netty消息传递
知识点
1、消息如何在管道中流转 ,一个管道中会有多个handler,当前的一个handler如何往下面的一个handler传递一个对象 主要通过handler往下传递对象的方法是sendUpstream(event) 2、看下粘包和分包是怎么样一个情况 hello hello 通过定义一个稳定的结构 length + hello 1、为什么FrameDecoder return的对象就是往下传递的对象 (还是调用了sendUpstream) 2、buffer里面数据未被读取完怎么办? (cumulation缓存) 3、为什么return null就可以缓存buffer (cumulation缓存) =============================分割线=========================== 3、FrameDecoder里面的cumulation其实就是一个缓存的buffer对象 包头+长度+数据 把长度定义的很大,这种数据包,通常被称为socket攻击,字节流式攻击
1、Client.java
package com.example.netty.lesson11.pipeLine; import java.net.Socket; public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("127.0.0.1", 51503); socket.getOutputStream().write("hello".getBytes()); socket.close(); } }
2、Server.java
package com.example.netty.lesson11.pipeLine; import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** * 消息传递 */ public class Server { public static void main(String[] args) { //服务类 ServerBootstrap bootstrap = new ServerBootstrap(); //boss线程监听端口,worker线程负责数据读写 ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker = Executors.newCachedThreadPool(); //设置niosocket工厂 bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker)); //设置管道的工厂 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("handler1", new MyHandler1()); pipeline.addLast("handler2", new MyHandler2()); return pipeline; } }); bootstrap.bind(new InetSocketAddress(51503)); System.out.println("start!!!"); } }
3、MyHandler1.java
package com.example.netty.lesson11.pipeLine; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.UpstreamMessageEvent; public class MyHandler1 extends SimpleChannelHandler { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { ChannelBuffer buffer = (ChannelBuffer)e.getMessage(); byte[] array = buffer.array(); String message = new String(array); System.out.println("handler1:" + message); //传递给handler2 ctx.sendUpstream(new UpstreamMessageEvent(ctx.getChannel(), "abc", e.getRemoteAddress())); ctx.sendUpstream(new UpstreamMessageEvent(ctx.getChannel(), "efg", e.getRemoteAddress())); } }
4、MyHandler2.java
package com.example.netty.lesson11.pipeLine; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; public class MyHandler2 extends SimpleChannelHandler { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { String message = (String)e.getMessage(); System.out.println("handler2:" + message); } }
完毕!
相关推荐
jannal 2020-06-21
xuMelon 2020-05-04
fengshantao 2020-10-29
arctan0 2020-10-14
爱传文档 2020-07-28
gzx0 2020-07-05
fengshantao 2020-07-04
fengshantao 2020-07-02
arctan0 2020-06-19
arctan0 2020-06-16
gzx0 2020-06-14
fengshantao 2020-06-13
gzx0 2020-06-12
arctan0 2020-06-11
fengshantao 2020-06-11
mbcsdn 2020-05-19
arctan0 2020-05-16