上课就犯困星人 2015-01-12 09:16 采纳率: 0%
浏览 5476
已结题

android使用netty框架与PC服务端通信,接收到内容与发送内容不同

例如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菜鸟,还请大神们帮忙看看

  • 写回答

3条回答 默认 最新

  • bdmh 移动开发领域优质创作者 2015-01-12 09:19
    关注

    你们的编码方式统一吗

    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画
  • ¥15 TeleScan不能修改bar
  • ¥100 请问我基于逐飞库写的这个有关于mp u6050传感器的函数,为什么输出的值是固定的?
  • ¥15 hadoop中启动hive报错如下怎么解决
  • ¥15 如何优化QWebEngineView 加载url的速度
  • ¥15 关于#hadoop#的问题,请各位专家解答!
  • ¥15 如何批量抓取网站信息
  • ¥15 Spring Boot离线人脸识别