From 0a4e8cea7bd32302a0da58d2a35c3c3eee4b6c2f Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Sat, 4 Jul 2020 19:00:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ESOCKET?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud-common-socket/pom.xml | 23 ++++++ .../src/main/java/com/cm/socket/Client.java | 74 +++++++++++++++++++ .../com/cm/socket/decoder/MessageDecoder.java | 52 +++++++++++++ .../com/cm/socket/encoder/MessageEncoder.java | 31 ++++++++ .../main/java/com/cm/socket/pojo/Message.java | 43 +++++++++++ pom.xml | 1 + 6 files changed, 224 insertions(+) create mode 100644 cloud-common-socket/pom.xml create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/Client.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java create mode 100644 cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java diff --git a/cloud-common-socket/pom.xml b/cloud-common-socket/pom.xml new file mode 100644 index 0000000..f462f45 --- /dev/null +++ b/cloud-common-socket/pom.xml @@ -0,0 +1,23 @@ + + + + cm-cloud + com.cm + 1.0.1-SNAPSHOT + + 4.0.0 + + cloud-common-socket + + + + io.netty + netty-all + 4.1.50.Final + + + + + \ No newline at end of file diff --git a/cloud-common-socket/src/main/java/com/cm/socket/Client.java b/cloud-common-socket/src/main/java/com/cm/socket/Client.java new file mode 100644 index 0000000..45600d0 --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/Client.java @@ -0,0 +1,74 @@ +package com.cm.socket; + +import com.cm.socket.decoder.MessageDecoder; +import com.cm.socket.encoder.MessageEncoder; +import com.cm.socket.pojo.Message; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: Client + * @Description: + * @Author: WangGeng + * @Date: 2020/7/4 18:20 + * @Version: 1.0 + **/ +public class Client { + + public static void main(String[] args) { + EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); + Bootstrap bootstrap = new Bootstrap(); + final int[] sendCount = {0}; + try { + bootstrap.group(eventLoopGroup); + bootstrap.channel(NioSocketChannel.class); + bootstrap.option(ChannelOption.SO_KEEPALIVE, true); + bootstrap.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new MessageEncoder()); + ch.pipeline().addLast(new MessageDecoder()); + ch.pipeline().addLast(new SimpleChannelInboundHandler() { + @Override + protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception { + System.out.println(msg.getContent()); + if(sendCount[0] < 4) { + Thread.sleep(5000); + Message message = new Message(); + message.setStart((byte) 0x01); + message.setType((byte) 0x02); + message.setContent("你好服务器"+ sendCount[0] +"!"); + ctx.channel().writeAndFlush(message); + sendCount[0]++; + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } + }); + } + }); + ChannelFuture channelFuture = bootstrap.connect("localhost", 9999).sync(); + Message message = new Message(); + message.setStart((byte) 0x01); + message.setType((byte) 0x02); + message.setContent("你好服务器!"); + channelFuture.channel().writeAndFlush(message); + channelFuture.channel().closeFuture().sync(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + eventLoopGroup.shutdownGracefully(); + } + } + +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java b/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java new file mode 100644 index 0000000..1cedcfa --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/decoder/MessageDecoder.java @@ -0,0 +1,52 @@ +package com.cm.socket.decoder; + +import com.cm.socket.pojo.Message; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: MessageDecoder + * @Description: 消息解码器 + * @Author: WangGeng + * @Date: 2020/7/4 16:11 + * @Version: 1.0 + **/ +public class MessageDecoder extends ByteToMessageDecoder { + /** + * 头部长度 1byte + 1byte + 4byte + */ + private static final int HEADER_LENGTH = 6; + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + if (in.readableBytes() < HEADER_LENGTH) { + return; + } + in.markReaderIndex(); + byte start = in.readByte(); + byte type = in.readByte(); + // 消息体长度 + int contentBytesLength = in.readInt(); + if (contentBytesLength <= 0) { + return; + } + if (in.readableBytes() < contentBytesLength) { + in.resetReaderIndex(); + return; + } + byte[] contentBytes = new byte[contentBytesLength]; + in.readBytes(contentBytes); + Message message = new Message(); + message.setStart(start); + message.setType(type); + message.setContent(new String(contentBytes, Charset.forName("UTF-8"))); + out.add(message); + } +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java b/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java new file mode 100644 index 0000000..63c0a1d --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/encoder/MessageEncoder.java @@ -0,0 +1,31 @@ +package com.cm.socket.encoder; + +import com.cm.socket.pojo.Message; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +import java.nio.charset.Charset; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: MessageEncoder + * @Description: 消息编码器 + * @Author: WangGeng + * @Date: 2020/7/4 15:55 + * @Version: 1.0 + **/ +public class MessageEncoder extends MessageToByteEncoder { + + @Override + protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception { + byte[] contentBytes = msg.getContent().getBytes(Charset.forName("UTF-8")); + out.writeByte(msg.getStart()); + out.writeByte(msg.getType()); + out.writeInt(contentBytes.length); + out.writeBytes(contentBytes); + } + +} diff --git a/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java b/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java new file mode 100644 index 0000000..4e0918b --- /dev/null +++ b/cloud-common-socket/src/main/java/com/cm/socket/pojo/Message.java @@ -0,0 +1,43 @@ +package com.cm.socket.pojo; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: Message + * @Description: 消息 + * @Author: WangGeng + * @Date: 2020/7/4 15:56 + * @Version: 1.0 + **/ +public class Message { + + private byte start; + private byte type; + private String content; + + public byte getStart() { + return start; + } + + public void setStart(byte start) { + this.start = start; + } + + public byte getType() { + return type; + } + + public void setType(byte type) { + this.type = type; + } + + public String getContent() { + return content == null ? "" : content.trim(); + } + + public void setContent(String content) { + this.content = content; + } + +} diff --git a/pom.xml b/pom.xml index 98b4187..80bea40 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ cloud-common-article cloud-common-plugin-sensitive cloud-common-freemarker + cloud-common-socket pom 成迈云