6

对接C++socket打包发送与解析,打包解包一律使用网络字节序(大端)java该怎么实现?

 3 years ago
source link: https://www.oschina.net/question/4894361_2320510
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

对接C++socket打包发送与解析,打包解包一律使用网络字节序(大端)java该怎么实现?

osc_66293634 发布于 前天 14:53
阅读 100

我这边java对接C++遇到些问题,首先是对方提供的文档没有例子,demo也是c++源码的,对方一直强调打包、网络字节序(大端),这块不是很懂,希望有大佬联系解决 

	
static void MakeHead(WORD cmd, char * buf, ULONG& len, UINT val=0)
{
    cmd = htons(cmd);
    unsigned long ulIndex = 0;
    /*<!-------------- Group Code ------------------->*/
    buf[ulIndex++] = (CHAR)0xDF;   
    short wn = ::htons(0x01);
    /*<!-------------- Command ID ------------------->*/
    // 第一个参数是目标地址,第二个参数是原地址,第三个参数是复制多少字节
    ::memcpy(&buf[ulIndex], &cmd, 2); ulIndex+=2; 
    /*<!---------------- Version -------------------->*/
    buf[ulIndex++] = 1; 
    /*<!------------- Command Flag ------------------>*/
    ulIndex+=4; 
    /*<!------------- Total Segment ----------------->*/
    wn = ::htons(0x01);
    //std::cout << "(network) total segment=" << wn << std::endl;
    ::memcpy(&buf[ulIndex], &wn, 2); ulIndex+=2;
    /*<!-------------- Sub Segment ------------------>*/
    wn = ::htons(0x01);
    //std::cout << "(network) sub segment=" << wn << std::endl;
    ::memcpy(&buf[ulIndex], &wn, 2); ulIndex+=2;
 
    /*<!------------- Segment  Flag ----------------->*/
    ulIndex+=2;
    /*<!--------------- Reserved1 ------------------->*/
    ulIndex+=2;
    /*<!--------------- Reserved2 ------------------->*/
    ulIndex+=4;
    /*<!---------------- Payload -------------------->*/
    if (cmd == htons(0x0A09))
    {
        val = htonl(val);
        memcpy(&buf[ulIndex], &val, 4); ulIndex+=4;
    }
    //输出字符a的地址
    len = ulIndex;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK