新增SOCKET

This commit is contained in:
wenc000 2020-07-04 19:00:01 +08:00
parent 8b8d77021d
commit 0a4e8cea7b
6 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cm-cloud</artifactId>
<groupId>com.cm</groupId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-common-socket</artifactId>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
</dependencies>
</project>

View File

@ -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<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new MessageEncoder());
ch.pipeline().addLast(new MessageDecoder());
ch.pipeline().addLast(new SimpleChannelInboundHandler<Message>() {
@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();
}
}
}

View File

@ -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<Object> 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);
}
}

View File

@ -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<Message> {
@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);
}
}

View File

@ -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;
}
}

View File

@ -21,6 +21,7 @@
<module>cloud-common-article</module>
<module>cloud-common-plugin-sensitive</module>
<module>cloud-common-freemarker</module>
<module>cloud-common-socket</module>
</modules>
<packaging>pom</packaging>
<description>成迈云</description>