例如PC端发送888,android端却收到888后面还跟着一大串以前测试时候的内容,
部分代码如下,复制时括号有些错乱请勿在意:
Bootstrap configureBootstrap(Bootstrap b, EventLoopGroup g) {
b.group(g)
.channel(NioSocketChannel.class)
.remoteAddress(parements.getString("ServerIP", host),
parements.getInt("port", port)) // 配置端口
.option(ChannelOption.TCP_NODELAY, true)
// .option(ChannelOption.SO_KEEPALIVE, true) // 1
.handler(new ChannelInitializer<SocketChannel>() { //有连接到达时会创建一个channel
@Override
public void initChannel(SocketChannel ch)
throws Exception { //pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
ch.pipeline().addLast(
new IdleStateHandler(READ_TIMEOUT, 3, 0),
decoderHandler, handler);
}
});
// Log.e("ConnService"," "+b.remoteAddress(parements.getString("ServerIP",
// host), parements.getInt("port", port)));
return b;
}
public class UptimeClientHandler extends ChannelDuplexHandler {
。。。。。。
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
sender = ctx;
println("Connected to: " + ctx.channel().remoteAddress());
RequestTools.sTerminal_Request_Online(parements.getInt("nMacID", 0), parements.getInt("pairID", 0));
}
@Override
public void read(ChannelHandlerContext ctx) throws Exception {
// ctx中包含的msg已经和发送内容不同
super.read(ctx);
sendOnline();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
super.channelReadComplete(ctx);
ctx.flush();
}
}
@Sharable
public class BasicCmdDecoder extends MessageToMessageDecoder<ByteBuf> @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf,
//buf接收到的内容与发送内容不同
List<Object> out) throws Exception {
synchronized (object) {
// TODO Auto-generated method stub
msg.writeBytes(buf, 0, buf.writerIndex());
remainLenght = msg.readableBytes();
if(msg.readableBytes()<5){
return;
}
略。。。
{
android菜鸟,还请大神们帮忙看看