feat(新增): 🚀 netty的 包的发送,分10次发送
This commit is contained in:
parent
89edfb6e4d
commit
26acd1c7c6
|
@ -9,11 +9,20 @@ import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
import io.netty.handler.logging.LogLevel;
|
||||||
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PackageClient {
|
public class PackageClient {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
// 分 10 次发送
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void send() {
|
||||||
NioEventLoopGroup worker = new NioEventLoopGroup();
|
NioEventLoopGroup worker = new NioEventLoopGroup();
|
||||||
try {
|
try {
|
||||||
Bootstrap bootstrap = new Bootstrap();
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
|
@ -22,15 +31,17 @@ public class PackageClient {
|
||||||
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
|
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel ch) throws Exception {
|
protected void initChannel(SocketChannel ch) throws Exception {
|
||||||
|
log.debug("conneted...");
|
||||||
|
ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
|
||||||
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
|
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
log.debug("sending...");
|
log.debug("sending...");
|
||||||
for (int i = 0; i < 10; i++) {
|
ByteBuf buffer = ctx.alloc().buffer();
|
||||||
ByteBuf buffer = ctx.alloc().buffer();
|
buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18});
|
||||||
buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
|
ctx.writeAndFlush(buffer);
|
||||||
ctx.writeAndFlush(buffer);
|
// 发完即关
|
||||||
}
|
ctx.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,9 @@ public class PackageServer {
|
||||||
ServerBootstrap serverBootstrap = new ServerBootstrap();
|
ServerBootstrap serverBootstrap = new ServerBootstrap();
|
||||||
serverBootstrap.channel(NioServerSocketChannel.class);
|
serverBootstrap.channel(NioServerSocketChannel.class);
|
||||||
serverBootstrap.group(boss, work);
|
serverBootstrap.group(boss, work);
|
||||||
// 接受缓冲区
|
// 接受缓冲区,接受为10个字节
|
||||||
serverBootstrap.option(ChannelOption.SO_RCVBUF, 10);
|
// serverBootstrap.option(ChannelOption.SO_RCVBUF, 10);
|
||||||
|
serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(16, 16, 16));
|
||||||
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
|
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||||
|
|
Loading…
Reference in New Issue