1:TimeServer类

 

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

public class TimeServer {

public void bind(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();

try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChildChannelHandler());
//绑定端口,同步等待成功
ChannelFuture future = serverBootstrap.bind(port).sync();
//等待服务端监听端口关闭
future.channel().closeFuture().sync();

} catch (Exception e) {
e.printStackTrace();
} finally {
//优雅退出
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}

private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new TimeServerHandler());
}
}

public static void main(String[] args) throws Exception {
int port = 8080;
if (args != null && args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
new TimeServer().bind(port);
}

}

 

 

 

 

 

2:TimeServerHandler

 

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeServerHandler extends ChannelInboundHandlerAdapter {

private int counter = 0;

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String)msg;
System.out.println("The time server receive order : " + body + "; the counter is " + (++this.counter));

String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";

currentTime += System.getProperty("line.separator");

ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
ctx.writeAndFlush(resp);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

 

 

 

3:TimeClient

 

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

public class TimeClient {

public void connect(int port, String host) throws Exception {
//配置客户端NIO线程组
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();

bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new TimeClientHandler());
}
});

//发起异步链接操作
ChannelFuture channelFuture = bootstrap.connect(host, port).sync();

//等待客户端链路关闭
channelFuture.channel().closeFuture().sync();

} catch (Exception e) {
e.printStackTrace();
} finally {
//优雅退出,释放NIO线程组
eventLoopGroup.shutdownGracefully();
}
}

public static void main(String[] args) throws Exception {
int port = 8080;
if (args != null && args.length > 0) {
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) { //采用默认值
e.printStackTrace();
}
}

new TimeClient().connect(port, "127.0.0.1");
}

}

 

 

 

4:TimeClientHandler

 

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class TimeClientHandler extends ChannelInboundHandlerAdapter {

private int counter = 0;

private byte[] req;

public TimeClientHandler() {
req = ("QUERY TIME ORDER" + System.getProperty("line.separator")).getBytes();
}

public void channelActive(ChannelHandlerContext ctx) {
ByteBuf message = null;
for (int i = 0; i < 100; i++) {
message = Unpooled.buffer(req.length);
message.writeBytes(req);
ctx.writeAndFlush(message);
}
}

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String)msg;
System.out.println("Now is " + body + "; the counter is " + (++this.counter));
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}

 

 

5:测试结果

TimeServer端:

The time server receive order : QUERY TIME ORDER; the counter is 1
The time server receive order : QUERY TIME ORDER; the counter is 2
The time server receive order : QUERY TIME ORDER; the counter is 3
The time server receive order : QUERY TIME ORDER; the counter is 4
The time server receive order : QUERY TIME ORDER; the counter is 5
The time server receive order : QUERY TIME ORDER; the counter is 6
The time server receive order : QUERY TIME ORDER; the counter is 7
The time server receive order : QUERY TIME ORDER; the counter is 8
The time server receive order : QUERY TIME ORDER; the counter is 9
The time server receive order : QUERY TIME ORDER; the counter is 10
The time server receive order : QUERY TIME ORDER; the counter is 11
The time server receive order : QUERY TIME ORDER; the counter is 12
The time server receive order : QUERY TIME ORDER; the counter is 13
The time server receive order : QUERY TIME ORDER; the counter is 14
The time server receive order : QUERY TIME ORDER; the counter is 15
The time server receive order : QUERY TIME ORDER; the counter is 16
The time server receive order : QUERY TIME ORDER; the counter is 17
The time server receive order : QUERY TIME ORDER; the counter is 18
The time server receive order : QUERY TIME ORDER; the counter is 19
The time server receive order : QUERY TIME ORDER; the counter is 20
The time server receive order : QUERY TIME ORDER; the counter is 21
The time server receive order : QUERY TIME ORDER; the counter is 22
The time server receive order : QUERY TIME ORDER; the counter is 23
The time server receive order : QUERY TIME ORDER; the counter is 24
The time server receive order : QUERY TIME ORDER; the counter is 25
The time server receive order : QUERY TIME ORDER; the counter is 26
The time server receive order : QUERY TIME ORDER; the counter is 27
The time server receive order : QUERY TIME ORDER; the counter is 28
The time server receive order : QUERY TIME ORDER; the counter is 29
The time server receive order : QUERY TIME ORDER; the counter is 30
The time server receive order : QUERY TIME ORDER; the counter is 31
The time server receive order : QUERY TIME ORDER; the counter is 32
The time server receive order : QUERY TIME ORDER; the counter is 33
The time server receive order : QUERY TIME ORDER; the counter is 34
The time server receive order : QUERY TIME ORDER; the counter is 35
The time server receive order : QUERY TIME ORDER; the counter is 36
The time server receive order : QUERY TIME ORDER; the counter is 37
The time server receive order : QUERY TIME ORDER; the counter is 38
The time server receive order : QUERY TIME ORDER; the counter is 39
The time server receive order : QUERY TIME ORDER; the counter is 40
The time server receive order : QUERY TIME ORDER; the counter is 41
The time server receive order : QUERY TIME ORDER; the counter is 42
The time server receive order : QUERY TIME ORDER; the counter is 43
The time server receive order : QUERY TIME ORDER; the counter is 44
The time server receive order : QUERY TIME ORDER; the counter is 45
The time server receive order : QUERY TIME ORDER; the counter is 46
The time server receive order : QUERY TIME ORDER; the counter is 47
The time server receive order : QUERY TIME ORDER; the counter is 48
The time server receive order : QUERY TIME ORDER; the counter is 49
The time server receive order : QUERY TIME ORDER; the counter is 50
The time server receive order : QUERY TIME ORDER; the counter is 51
The time server receive order : QUERY TIME ORDER; the counter is 52
The time server receive order : QUERY TIME ORDER; the counter is 53
The time server receive order : QUERY TIME ORDER; the counter is 54
The time server receive order : QUERY TIME ORDER; the counter is 55
The time server receive order : QUERY TIME ORDER; the counter is 56
The time server receive order : QUERY TIME ORDER; the counter is 57
The time server receive order : QUERY TIME ORDER; the counter is 58
The time server receive order : QUERY TIME ORDER; the counter is 59
The time server receive order : QUERY TIME ORDER; the counter is 60
The time server receive order : QUERY TIME ORDER; the counter is 61
The time server receive order : QUERY TIME ORDER; the counter is 62
The time server receive order : QUERY TIME ORDER; the counter is 63
The time server receive order : QUERY TIME ORDER; the counter is 64
The time server receive order : QUERY TIME ORDER; the counter is 65
The time server receive order : QUERY TIME ORDER; the counter is 66
The time server receive order : QUERY TIME ORDER; the counter is 67
The time server receive order : QUERY TIME ORDER; the counter is 68
The time server receive order : QUERY TIME ORDER; the counter is 69
The time server receive order : QUERY TIME ORDER; the counter is 70
The time server receive order : QUERY TIME ORDER; the counter is 71
The time server receive order : QUERY TIME ORDER; the counter is 72
The time server receive order : QUERY TIME ORDER; the counter is 73
The time server receive order : QUERY TIME ORDER; the counter is 74
The time server receive order : QUERY TIME ORDER; the counter is 75
The time server receive order : QUERY TIME ORDER; the counter is 76
The time server receive order : QUERY TIME ORDER; the counter is 77
The time server receive order : QUERY TIME ORDER; the counter is 78
The time server receive order : QUERY TIME ORDER; the counter is 79
The time server receive order : QUERY TIME ORDER; the counter is 80
The time server receive order : QUERY TIME ORDER; the counter is 81
The time server receive order : QUERY TIME ORDER; the counter is 82
The time server receive order : QUERY TIME ORDER; the counter is 83
The time server receive order : QUERY TIME ORDER; the counter is 84
The time server receive order : QUERY TIME ORDER; the counter is 85
The time server receive order : QUERY TIME ORDER; the counter is 86
The time server receive order : QUERY TIME ORDER; the counter is 87
The time server receive order : QUERY TIME ORDER; the counter is 88
The time server receive order : QUERY TIME ORDER; the counter is 89
The time server receive order : QUERY TIME ORDER; the counter is 90
The time server receive order : QUERY TIME ORDER; the counter is 91
The time server receive order : QUERY TIME ORDER; the counter is 92
The time server receive order : QUERY TIME ORDER; the counter is 93
The time server receive order : QUERY TIME ORDER; the counter is 94
The time server receive order : QUERY TIME ORDER; the counter is 95
The time server receive order : QUERY TIME ORDER; the counter is 96
The time server receive order : QUERY TIME ORDER; the counter is 97
The time server receive order : QUERY TIME ORDER; the counter is 98
The time server receive order : QUERY TIME ORDER; the counter is 99
The time server receive order : QUERY TIME ORDER; the counter is 100

 

TimeClient端:

Now is Mon Jul 10 00:55:19 CST 2017; the counter is 1
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 2
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 3
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 4
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 5
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 6
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 7
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 8
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 9
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 10
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 11
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 12
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 13
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 14
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 15
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 16
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 17
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 18
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 19
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 20
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 21
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 22
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 23
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 24
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 25
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 26
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 27
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 28
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 29
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 30
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 31
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 32
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 33
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 34
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 35
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 36
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 37
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 38
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 39
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 40
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 41
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 42
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 43
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 44
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 45
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 46
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 47
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 48
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 49
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 50
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 51
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 52
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 53
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 54
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 55
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 56
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 57
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 58
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 59
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 60
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 61
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 62
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 63
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 64
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 65
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 66
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 67
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 68
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 69
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 70
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 71
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 72
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 73
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 74
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 75
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 76
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 77
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 78
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 79
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 80
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 81
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 82
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 83
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 84
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 85
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 86
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 87
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 88
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 89
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 90
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 91
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 92
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 93
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 94
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 95
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 96
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 97
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 98
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 99
Now is Mon Jul 10 00:55:19 CST 2017; the counter is 100

 

利用LineBasedFrameDecoder解决TCP粘包问题_.net

越努力,越幸运,欢迎关注我的个人技术公众号