码迷,mamicode.com
首页 > 编程语言 > 详细

Java网络消息交互,对响应的byte消息的典型解析:

时间:2015-03-03 11:45:12      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:java

// 接收byte响应   

 public static RecevEntity getRetVo(byte[] data)
    {
  ByteBuffer buf = ByteBuffer.wrap(data);
  buf.order(ConstantSelf.AgentMsgByteSet);
  SelfQryRspVO uvo = new SelfQryRspVO();

  {
   byte[] cisdn = new byte[21];
   buf.get(cisdn);
   uvo.setIsdn(new String(cisdn, ConstantSelf.AgentMsgEncoding).toString().trim());
   uvo.setDwseq(buf.getInt());
   uvo.setBuflen(buf.getInt());

   uvo.setResult(buf.getInt());
  }
 }

// 打包发送byte数据

    public static byte[] getBytes(int msgFlag, int type,String fileName)
    {
        // 发送的消息总长度
        int msgtotallen = 4 +
                1 +
                256;
        ByteBuffer buf = ByteBuffer.allocate(msgtotallen);
        // 设置字节序
        buf.order(ByteOrder.LITTLE_ENDIAN);

        try
        {
            buf.putInt(msgFlag);
            buf.put((byte)type);
            byte[] bmdn = new byte[256];
            byte[] smdn = fileName.getBytes("GBK");
            System.arraycopy(smdn, 0, bmdn, 0, smdn.length);
            buf.put(bmdn);
        }
        catch(Exception ex)
        {
         uniportal_logger.error(ex.toString());
        }
        return buf.array();
    }


// 打印为十六进制可读字符串

    public static String bytesToHex(final byte[] bytes)
    {
        if(bytes == null)
        {
            return "";
        }
        StringBuffer strBuffer = new StringBuffer(bytes.length * 3);
        for(int i = 0;i < bytes.length;i++)
        {
            strBuffer.append(Integer.toHexString(bytes[i] & 0xff));
            strBuffer.append(" ");
        }
        return strBuffer.toString();
    }

 

Java网络消息交互,对响应的byte消息的典型解析:

标签:java

原文地址:http://blog.csdn.net/liu76xt/article/details/44035305

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!