如果在沒有發送資料 只是連接請求可以看到連接數在3w多
window環境下運行,我的代碼如下:
private void start() {
logHelper.info("Start server.....");
System.out.println("Start server.....");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
try {
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
//final String port = ResourceUtil.getKey("local_tcp_port");
//final String webPort = ResourceUtil.getKey("local_web_port");
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {
try {
//ByteBuf delimiter = Unpooled.copiedBuffer("".getBytes());
//ByteBuf bb = Unpooled.buffer(0, 2048);
channel.config().setRecvByteBufAllocator(new AdaptiveRecvByteBufAllocator(4, 2048, Integer.MAX_VALUE));
ChannelPipeline pipeline = channel.pipeline();
//IdleStateHandler心跳機制,如果超時觸發Handle中userEventTrigger()方法
pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.MINUTES));//5分鐘心跳
//pipeline.addLast(new CustomDecoder(2048,0,0,0,0,false));
pipeline.addLast(new MyDecoder(2048));
// //過濾編碼
// pipeline.addLast("decoder", new ByteArrayDecoder());
// //過濾編碼
// pipeline.addLast("encoder", new ByteArrayEncoder());
InetSocketAddress inet = (InetSocketAddress)channel.localAddress();
if(inet.getPort()== Integer.parseInt(SMSTool.local_tcp_port))
pipeline.addLast(new ServerHandler());
// else if(inet.getPort() == Integer.parseInt(SMSTool.local_web_port))
// pipeline.addLast(new WebServerHandler(initCallQueue));
} catch (Exception e) {
// TODO: handle exception
//LogHelper.getIntance("dsd").equals(e.toString());
logHelper.error(e.toString());
}
}
}).option(ChannelOption.SO_BACKLOG, 1024)
//.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535))
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 系結埠,并且回傳ChannelFuture物件
ChannelFuture future = bootstrap.bind(Integer.parseInt(SMSTool.local_tcp_port)).sync();
ChannelFuture future2 = bootstrap.bind(Integer.parseInt(SMSTool.local_web_port)).sync();
System.out.println("server started!");
logHelper.info("server started!");
getConnectionTotal();
// 等待客戶端的關閉
//Channel channel =
future.channel().closeFuture().sync();
future2.channel().closeFuture().sync();
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);//記憶體泄漏檢測 開發推薦PARANOID 線上SIMPLE
// 要記得是closeFuture(),作用是等待服務端的關閉
//channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully().syncUninterruptibly();
workerGroup.shutdownGracefully().syncUninterruptibly();
}
}
編譯器代碼:
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
try {
int readable = buffer.readableBytes();
if(readable > MAX_FRAME_SIZE) { // 緩沖區資料過大
buffer.skipBytes(readable); // 忽略所有可讀的位元組
// 拋出例外通知這個幀資料超長
throw new TooLongFrameException("幀資料超長");
}
//創建位元組陣列,buffer.readableBytes可讀位元組長度
byte[] b = new byte[readable];
//復制內容到位元組陣列b
buffer.readBytes(b);
out.add(buffer);
} catch (Exception e) {
// TODO: handle exception
LogHelper.getIntance().error("decode=>"+e.toString());
}
}
業務處理類:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// TODO Auto-generated method stub
ByteBuf buf = (ByteBuf)msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
byte[]req2 = new byte[req.length+2];
System.arraycopy(req, 0, req2, 0, req.length);
byte[] bb = Convert.shortToByte(SMSTool.GT06_FOOTER);
req2[req2.length-2] = bb[0];
req2[req2.length-1] = bb[1];
super.channelRead(ctx, req2);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
try {
ctx.channel().close();
//super.channelInactive(ctx);
//total.getList().remove(ctx.channel().remoteAddress().toString());
total.getConnection().decrementAndGet();//記錄連接數
System.out.println("disconnect->"+ctx.channel().remoteAddress().toString());//斷開連接
} catch (Exception e) {
// TODO: handle exception
logHelper.error(e.toString());
}
}
這個是服務端的主要代碼,請高手指教!!!!!!
uj5u.com熱心網友回復:
這個問題已經找到原因了,自己結貼轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/96020.html
標籤:Java SE
上一篇:JAVA結合AE(Adobe After Effects)實作類似于傳影(視頻DIY)的核心功能,程式也可以是藝術。
下一篇:泛型向上向下轉型
