知識點
1、看下粘包和分包是怎么樣一個情況 hello hello 通過定義一個穩定的結構 length + hello 2、buffer里面數據未被讀取完怎么辦? (cumulation緩存) 3、為什么return null就可以緩存buffer (cumulation緩存)
1、Client.java
package com.example.netty.lesson11.packet; import java.net.Socket; import java.nio.ByteBuffer; public class Client { public static void main(String[] args) throws Exception { /** * 會出現粘包現象,通過ByteBuffer解決粘包問題 */ Socket socket = new Socket("127.0.0.1", 51503); String message = "hello,ni hao ma?"; byte[] bytes = message.getBytes(); //4定義為包頭位數 ByteBuffer buffer = ByteBuffer.allocate(4+bytes.length); buffer.putInt(bytes.length); buffer.put(bytes); byte[] array = buffer.array(); for (int i = 0; i <1000 ; i++) { socket.getOutputStream().write(array); } socket.close(); } }
2、Server.java
package com.example.netty.lesson11.packet; import com.example.netty.lesson11.pipeLine.MyHandler2; 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; import java.net.InetSocketAddress; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 消息傳遞 */ 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("decoder", new MyDecoder()); pipeline.addLast("handler1", new MyHandler1()); return pipeline; } }); bootstrap.bind(new InetSocketAddress(51503)); System.out.println("start!!!"); } }
3、MyDecoder.java
package com.example.netty.lesson11.packet; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; /** * @author yangwj * @date 2020/4/6 10:48 */ public class MyDecoder extends FrameDecoder { @Override protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer buffer) throws Exception { if(buffer.readableBytes() >4){ //標記 buffer.markReaderIndex(); //長度 int length = buffer.readInt(); if(buffer.readableBytes() < length){ buffer.resetReaderIndex(); //緩存當前剩余的buffer數據,等待剩下的數據到來 return null; } //讀數據 byte[] bytes = new byte[length]; buffer.readBytes(bytes); //往下傳遞 ,和sendUpstream類似 return new String(bytes); } //緩存當前剩余的buffer數據,等待剩下的數據到來 return null; } }
4、MyHandler1.java
package com.example.netty.lesson11.packet; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; 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); String message = (String) e.getMessage(); System.out.println("handler1:" + message); } }
完畢!