如何使用java传入给c服务端结构体


在java和C的socket交互中 是用的是结构体传输   java怎么传  其实很简单  c中并没有Java的对象  他的结构体 你只要按照c中结构体的字段 转成字节数组就好了  

比如 

public byte[] MsgToBuf (MsgHead msgHead){
    	 byte[] buf = new byte[20];
    	 int msgh = msgHead.getMsgh();
    	 int msgid = msgHead.getMsgid();
    	 int cmd = msgHead.getCmd();
    	 int error = msgHead.getError();
    	 int dataL = msgHead.getDataL();
    			byte[] temp = clientUtils.intToByteArray(msgh);
//    			byte[] temp = clientUtils.tolh(msgh);
    			System.arraycopy(temp, 0, buf, 0, temp.length);

    			temp = clientUtils.intToByteArray(msgid);
//    			temp = clientUtils.tolh(msgid);
    			System.arraycopy(temp, 0, buf, 4, temp.length);

    			temp = clientUtils.intToByteArray(cmd);
//    			temp = clientUtils.tolh(cmd);
    			System.arraycopy(temp, 0, buf, 8, temp.length);
    			
    			temp = clientUtils.intToByteArray(error);
//    			temp = clientUtils.tolh(error);
    			System.arraycopy(temp, 0, buf, 12, temp.length);
    			
    			temp = clientUtils.intToByteArray(dataL);
//    			temp = clientUtils.tolh(dataL);
    			System.arraycopy(temp, 0, buf, 16, temp.length);
    			
    			return buf;
    }


他是直接开辟字节数组的大小 直接放到数组中就可以的  int转byte的方法 我在其他的博客中有写到     string直接转byte传就好了  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值