以下代码仅仅是demo。不适用于正式环境
消息的定义
public class NettyMessage {
private Header header;
private String data;
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "NettyMessage{" +
"header=" + header +
", data='" + data + '\'' +
'}';
}
}
public class Header {
private int length;
private int type;
private int mainVersion;
private int subVersion;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getMainVersion() {
return mainVersion;
}
public void setMainVersion(int mainVersion) {
this.mainVersion = mainVersion;
}
public int getSubVersion() {
return subVersion;
}
public void setSubVersion(int subVersion) {
this.subVersion = subVersion;
}
@Override
public String toString() {
return "Header{" +
"length=" + length +
", type=" + type +
", mainVersion=" + mainVersion +
", subVersion=" + subVersion +
'}';
}
}
public class MessageDecode extends MessageToMessageDecoder<ByteBuf> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
int length = msg.readInt();
int type = msg.readInt();
int mainVersion = msg.readInt();
int subVersion = msg.readInt();
int dataLength = msg.readInt();
byte[] data =new byte[dataLength];
msg.readBytes(data);
String strData = new String(data, Charset.forName("UTF-8"));
NettyMessage nettyMessage = new NettyMessage();
Header header = new Header();
nettyMessage.setHeader(header);
nettyMessage.setData(strData);
header.setLength(length);
header.setMainVersion(mainVersion);
header.setSubVersion(subVersion);
header.setType(type);
}
}
public class MessageEncode extends MessageToByteEncoder<NettyMessage> {
@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf out) throws Exception {
ByteBuf sendBuffer = Unpooled.buffer(); //
final Header header = msg.getHeader();
sendBuffer.writeInt(header.getLength());
sendBuffer.writeInt(header.getType());
sendBuffer.writeInt(header.getMainVersion());
sendBuffer.writeInt(header.getSubVersion());
sendBuffer.writeInt(msg.getData().getBytes().length);
sendBuffer.writeBytes(msg.getData().getBytes());
out.writeInt(sendBuffer.readableBytes());
out.writeBytes(sendBuffer);
ctx.flush();
}
}
public class MainServer {
static class ServerHandler extends SimpleChannelInboundHandler<NettyMessage> {
@Override
protected void messageReceived(ChannelHandlerContext ctx, NettyMessage msg) throws Exception {
System.out.println(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
}
public static void main(String[] args) {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup workers = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, workers)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline cpl = ch.pipeline();
cpl.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, 0, 4));
cpl.addLast(new MessageDecode());
cpl.addLast(new MessageEncode());
cpl.addLast(new ServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind("localhost", 9999).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
workers.shutdownGracefully();
}
}
}
public class MainClient {
static class ClientServer extends SimpleChannelInboundHandler<ByteBuf> {
@Override
protected void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
byte[] buffer = new byte[msg.readableBytes()];
msg.readBytes(buffer);
System.out.println("客户端收到消息:" + new String(buffer, Charset.forName("UTF-8")));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 1; i < 100; i++) {
NettyMessage nettyMessage = new NettyMessage();
nettyMessage.setData("你好,现在是第:" + i + "次");
Header header = new Header();
header.setType(i);
header.setLength(20);
header.setMainVersion(1);
header.setSubVersion(1);
nettyMessage.setHeader(header);
ctx.writeAndFlush(nettyMessage);
}
}
}
public static void main(String[] args) {
EventLoopGroup workers = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.group(workers)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
/**
*
* 1024: 消息的最大长度
* 0: 长度的起始偏移量
* 4: 长度属性占的字节数
* 0: 长度调节值,在总长被定义为包含包头长度时,修正信息长度
* 4: 跳过的字节数,根据需要我们跳过前4个字节,以便接收端直接接受到不含“长度属性
*
*/
channelPipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, 0, 4));
channelPipeline.addLast(new MessageDecode());
channelPipeline.addLast(new MessageEncode());
channelPipeline.addLast(new ClientServer());
}
});
ChannelFuture channelFuture = bootstrap.connect("localhost", 9999).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workers.shutdownGracefully();
}
}
}
服务端输出:
NettyMessage{header=Header{length=20, type=1, mainVersion=1, subVersion=1}, data='你好,现在是第:1次'}
NettyMessage{header=Header{length=20, type=2, mainVersion=1, subVersion=1}, data='你好,现在是第:2次'}
NettyMessage{header=Header{length=20, type=3, mainVersion=1, subVersion=1}, data='你好,现在是第:3次'}
NettyMessage{header=Header{length=20, type=4, mainVersion=1, subVersion=1}, data='你好,现在是第:4次'}
NettyMessage{header=Header{length=20, type=5, mainVersion=1, subVersion=1}, data='你好,现在是第:5次'}
NettyMessage{header=Header{length=20, type=6, mainVersion=1, subVersion=1}, data='你好,现在是第:6次'}
NettyMessage{header=Header{length=20, type=7, mainVersion=1, subVersion=1}, data='你好,现在是第:7次'}
NettyMessage{header=Header{length=20, type=8, mainVersion=1, subVersion=1}, data='你好,现在是第:8次'}
NettyMessage{header=Header{length=20, type=9, mainVersion=1, subVersion=1}, data='你好,现在是第:9次'}
NettyMessage{header=Header{length=20, type=10, mainVersion=1, subVersion=1}, data='你好,现在是第:10次'}
NettyMessage{header=Header{length=20, type=11, mainVersion=1, subVersion=1}, data='你好,现在是第:11次'}
NettyMessage{header=Header{length=20, type=12, mainVersion=1, subVersion=1}, data='你好,现在是第:12次'}
NettyMessage{header=Header{length=20, type=13, mainVersion=1, subVersion=1}, data='你好,现在是第:13次'}
NettyMessage{header=Header{length=20, type=14, mainVersion=1, subVersion=1}, data='你好,现在是第:14次'}
NettyMessage{header=Header{length=20, type=15, mainVersion=1, subVersion=1}, data='你好,现在是第:15次'}
NettyMessage{header=Header{length=20, type=16, mainVersion=1, subVersion=1}, data='你好,现在是第:16次'}
NettyMessage{header=Header{length=20, type=17, mainVersion=1, subVersion=1}, data='你好,现在是第:17次'}
NettyMessage{header=Header{length=20, type=18, mainVersion=1, subVersion=1}, data='你好,现在是第:18次'}
NettyMessage{header=Header{length=20, type=19, mainVersion=1, subVersion=1}, data='你好,现在是第:19次'}
NettyMessage{header=Header{length=20, type=20, mainVersion=1, subVersion=1}, data='你好,现在是第:20次'}
NettyMessage{header=Header{length=20, type=21, mainVersion=1, subVersion=1}, data='你好,现在是第:21次'}
NettyMessage{header=Header{length=20, type=22, mainVersion=1, subVersion=1}, data='你好,现在是第:22次'}
NettyMessage{header=Header{length=20, type=23, mainVersion=1, subVersion=1}, data='你好,现在是第:23次'}
NettyMessage{header=Header{length=20, type=24, mainVersion=1, subVersion=1}, data='你好,现在是第:24次'}
NettyMessage{header=Header{length=20, type=25, mainVersion=1, subVersion=1}, data='你好,现在是第:25次'}
NettyMessage{header=Header{length=20, type=26, mainVersion=1, subVersion=1}, data='你好,现在是第:26次'}
NettyMessage{header=Header{length=20, type=27, mainVersion=1, subVersion=1}, data='你好,现在是第:27次'}
NettyMessage{header=Header{length=20, type=28, mainVersion=1, subVersion=1}, data='你好,现在是第:28次'}
NettyMessage{header=Header{length=20, type=29, mainVersion=1, subVersion=1}, data='你好,现在是第:29次'}
NettyMessage{header=Header{length=20, type=30, mainVersion=1, subVersion=1}, data='你好,现在是第:30次'}
NettyMessage{header=Header{length=20, type=31, mainVersion=1, subVersion=1}, data='你好,现在是第:31次'}
NettyMessage{header=Header{length=20, type=32, mainVersion=1, subVersion=1}, data='你好,现在是第:32次'}
NettyMessage{header=Header{length=20, type=33, mainVersion=1, subVersion=1}, data='你好,现在是第:33次'}
NettyMessage{header=Header{length=20, type=34, mainVersion=1, subVersion=1}, data='你好,现在是第:34次'}
NettyMessage{header=Header{length=20, type=35, mainVersion=1, subVersion=1}, data='你好,现在是第:35次'}
NettyMessage{header=Header{length=20, type=36, mainVersion=1, subVersion=1}, data='你好,现在是第:36次'}
NettyMessage{header=Header{length=20, type=37, mainVersion=1, subVersion=1}, data='你好,现在是第:37次'}
NettyMessage{header=Header{length=20, type=38, mainVersion=1, subVersion=1}, data='你好,现在是第:38次'}
NettyMessage{header=Header{length=20, type=39, mainVersion=1, subVersion=1}, data='你好,现在是第:39次'}
NettyMessage{header=Header{length=20, type=40, mainVersion=1, subVersion=1}, data='你好,现在是第:40次'}
NettyMessage{header=Header{length=20, type=41, mainVersion=1, subVersion=1}, data='你好,现在是第:41次'}
NettyMessage{header=Header{length=20, type=42, mainVersion=1, subVersion=1}, data='你好,现在是第:42次'}
NettyMessage{header=Header{length=20, type=43, mainVersion=1, subVersion=1}, data='你好,现在是第:43次'}
NettyMessage{header=Header{length=20, type=44, mainVersion=1, subVersion=1}, data='你好,现在是第:44次'}
NettyMessage{header=Header{length=20, type=45, mainVersion=1, subVersion=1}, data='你好,现在是第:45次'}
NettyMessage{header=Header{length=20, type=46, mainVersion=1, subVersion=1}, data='你好,现在是第:46次'}
NettyMessage{header=Header{length=20, type=47, mainVersion=1, subVersion=1}, data='你好,现在是第:47次'}
NettyMessage{header=Header{length=20, type=48, mainVersion=1, subVersion=1}, data='你好,现在是第:48次'}
NettyMessage{header=Header{length=20, type=49, mainVersion=1, subVersion=1}, data='你好,现在是第:49次'}
NettyMessage{header=Header{length=20, type=50, mainVersion=1, subVersion=1}, data='你好,现在是第:50次'}
NettyMessage{header=Header{length=20, type=51, mainVersion=1, subVersion=1}, data='你好,现在是第:51次'}
NettyMessage{header=Header{length=20, type=52, mainVersion=1, subVersion=1}, data='你好,现在是第:52次'}
NettyMessage{header=Header{length=20, type=53, mainVersion=1, subVersion=1}, data='你好,现在是第:53次'}
NettyMessage{header=Header{length=20, type=54, mainVersion=1, subVersion=1}, data='你好,现在是第:54次'}
NettyMessage{header=Header{length=20, type=55, mainVersion=1, subVersion=1}, data='你好,现在是第:55次'}
NettyMessage{header=Header{length=20, type=56, mainVersion=1, subVersion=1}, data='你好,现在是第:56次'}
NettyMessage{header=Header{length=20, type=57, mainVersion=1, subVersion=1}, data='你好,现在是第:57次'}
NettyMessage{header=Header{length=20, type=58, mainVersion=1, subVersion=1}, data='你好,现在是第:58次'}
NettyMessage{header=Header{length=20, type=59, mainVersion=1, subVersion=1}, data='你好,现在是第:59次'}
NettyMessage{header=Header{length=20, type=60, mainVersion=1, subVersion=1}, data='你好,现在是第:60次'}
NettyMessage{header=Header{length=20, type=61, mainVersion=1, subVersion=1}, data='你好,现在是第:61次'}
NettyMessage{header=Header{length=20, type=62, mainVersion=1, subVersion=1}, data='你好,现在是第:62次'}
NettyMessage{header=Header{length=20, type=63, mainVersion=1, subVersion=1}, data='你好,现在是第:63次'}
NettyMessage{header=Header{length=20, type=64, mainVersion=1, subVersion=1}, data='你好,现在是第:64次'}
NettyMessage{header=Header{length=20, type=65, mainVersion=1, subVersion=1}, data='你好,现在是第:65次'}
NettyMessage{header=Header{length=20, type=66, mainVersion=1, subVersion=1}, data='你好,现在是第:66次'}
NettyMessage{header=Header{length=20, type=67, mainVersion=1, subVersion=1}, data='你好,现在是第:67次'}
NettyMessage{header=Header{length=20, type=68, mainVersion=1, subVersion=1}, data='你好,现在是第:68次'}
NettyMessage{header=Header{length=20, type=69, mainVersion=1, subVersion=1}, data='你好,现在是第:69次'}
NettyMessage{header=Header{length=20, type=70, mainVersion=1, subVersion=1}, data='你好,现在是第:70次'}
NettyMessage{header=Header{length=20, type=71, mainVersion=1, subVersion=1}, data='你好,现在是第:71次'}
NettyMessage{header=Header{length=20, type=72, mainVersion=1, subVersion=1}, data='你好,现在是第:72次'}
NettyMessage{header=Header{length=20, type=73, mainVersion=1, subVersion=1}, data='你好,现在是第:73次'}
NettyMessage{header=Header{length=20, type=74, mainVersion=1, subVersion=1}, data='你好,现在是第:74次'}
NettyMessage{header=Header{length=20, type=75, mainVersion=1, subVersion=1}, data='你好,现在是第:75次'}
NettyMessage{header=Header{length=20, type=76, mainVersion=1, subVersion=1}, data='你好,现在是第:76次'}
NettyMessage{header=Header{length=20, type=77, mainVersion=1, subVersion=1}, data='你好,现在是第:77次'}
NettyMessage{header=Header{length=20, type=78, mainVersion=1, subVersion=1}, data='你好,现在是第:78次'}
NettyMessage{header=Header{length=20, type=79, mainVersion=1, subVersion=1}, data='你好,现在是第:79次'}
NettyMessage{header=Header{length=20, type=80, mainVersion=1, subVersion=1}, data='你好,现在是第:80次'}
NettyMessage{header=Header{length=20, type=81, mainVersion=1, subVersion=1}, data='你好,现在是第:81次'}
NettyMessage{header=Header{length=20, type=82, mainVersion=1, subVersion=1}, data='你好,现在是第:82次'}
NettyMessage{header=Header{length=20, type=83, mainVersion=1, subVersion=1}, data='你好,现在是第:83次'}
NettyMessage{header=Header{length=20, type=84, mainVersion=1, subVersion=1}, data='你好,现在是第:84次'}
NettyMessage{header=Header{length=20, type=85, mainVersion=1, subVersion=1}, data='你好,现在是第:85次'}
NettyMessage{header=Header{length=20, type=86, mainVersion=1, subVersion=1}, data='你好,现在是第:86次'}
NettyMessage{header=Header{length=20, type=87, mainVersion=1, subVersion=1}, data='你好,现在是第:87次'}
NettyMessage{header=Header{length=20, type=88, mainVersion=1, subVersion=1}, data='你好,现在是第:88次'}
NettyMessage{header=Header{length=20, type=89, mainVersion=1, subVersion=1}, data='你好,现在是第:89次'}
NettyMessage{header=Header{length=20, type=90, mainVersion=1, subVersion=1}, data='你好,现在是第:90次'}
NettyMessage{header=Header{length=20, type=91, mainVersion=1, subVersion=1}, data='你好,现在是第:91次'}
NettyMessage{header=Header{length=20, type=92, mainVersion=1, subVersion=1}, data='你好,现在是第:92次'}
NettyMessage{header=Header{length=20, type=93, mainVersion=1, subVersion=1}, data='你好,现在是第:93次'}
NettyMessage{header=Header{length=20, type=94, mainVersion=1, subVersion=1}, data='你好,现在是第:94次'}
NettyMessage{header=Header{length=20, type=95, mainVersion=1, subVersion=1}, data='你好,现在是第:95次'}
NettyMessage{header=Header{length=20, type=96, mainVersion=1, subVersion=1}, data='你好,现在是第:96次'}
NettyMessage{header=Header{length=20, type=97, mainVersion=1, subVersion=1}, data='你好,现在是第:97次'}
NettyMessage{header=Header{length=20, type=98, mainVersion=1, subVersion=1}, data='你好,现在是第:98次'}
NettyMessage{header=Header{length=20, type=99, mainVersion=1, subVersion=1}, data='你好,现在是第:99次'}