注册消息码

// exception.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <exception>
#include <vector>
#include <windows.h>
#include <list>
#include <vector>

using namespace std;

typedef struct client_t{
    int fd;
    int iMode;
    std::string file;
}CLIENT_T;


typedef struct cliInfo{
    cliInfo(int fd,int iMode, string file) : fd(fd), iMode(iMode), file(file){}
    void clearRegCode()
    {
        lsMsgCode.clear();
    }

    void delMsgCode(int msgcode)
    {
        list<int>::iterator it = lsMsgCode.begin();
        while(it != lsMsgCode.end())
        {
           // if();
        }
    }

    bool addMsgCode(int code)
    {
        if(isExist(code))
        {
            return false;
        }
        else
        {
            lsMsgCode.push_back(code);
            printf("add code %d \n", code);
            return true;
        }

    }

    bool isExist(int code)
    {
        list<int>::iterator it = lsMsgCode.begin();
        while(it != lsMsgCode.end())
        {
            if(code == *it)
            {
                return true;
            }
            ++it;
        }
        return false;
    }


    int fd;
    int iMode;
    std::string file;
    std::list<int> lsMsgCode;
}CLIINFO_T;

class CClientsManager{
public:
    CClientsManager(){}
    ~CClientsManager(){}

    bool addClient(CLIINFO_T cliifo)
    {
        list<CLIINFO_T>::iterator it = registed.begin();
       
        while(it != registed.end())
        {
            if(cliifo.fd == it->fd)
            {
                printf("该fd已存在,注册失败。\n");
                return false;
            }
            ++it;
        }

        registed.push_back(cliifo);
        printf("fd:%d已经注册成功\n", cliifo.fd);
        return true;
    }

    bool delClient(int fd)
    {

        return true;
    }

    //给客户端注册消息码
    bool clientRegMsgCode(int fd, int msgcode)
    {
        list<CLIINFO_T>::iterator it = registed.begin();
        while(it!=registed.end())
        {
            ++it;
        }
    }

    //现实所有客户端注册信息
    void showClientInfo()
    {
        list<CLIINFO_T>::iterator it = registed.begin();
        while(it != registed.end())
        {
            printf("client[1] : fd = %d, iMode = %d, file = %s. reg Code:", it->fd, it->iMode, it->file.c_str());
            list<int>::iterator msgCodeIt = it->lsMsgCode.begin();
            while(msgCodeIt!=it->lsMsgCode.end())
            {
                printf("%d ", *msgCodeIt);
                ++msgCodeIt;
            }

            ++it;
        }
    }
   
private:

    std::list<CLIINFO_T> registed;
};

int _tmain(int argc, _TCHAR* argv[])
{
    CClientsManager clients;

    CLIINFO_T cliifo(1,100,"socket1");

    cliifo.addMsgCode(1);
    cliifo.addMsgCode(2);
    cliifo.addMsgCode(3);
    cliifo.addMsgCode(4);
    cliifo.addMsgCode(5);

    clients.addClient(cliifo);

    clients.showClientInfo();

    Sleep(10);
 
    exit(0);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JT808是中国车载移动通信标准,用于车辆监控、定位、调度等领域。其中注册鉴权报文是在车辆终端与上级平台建立连接时的必要流程,本文将介绍如何使用C语言解析JT808注册鉴权报文。 首先,我们需要了解JT808注册鉴权报文的格式。其数据包含有消息ID、消息体属性、终端手机号、鉴权等字段。具体格式如下: | 消息ID | 消息体属性 | 终端手机号 | 鉴权 | |:-----:|:--------:|:-------:|:----:| | 0x0102 | 2字节 | 6字节 | 不定长 | 其中,消息ID固定为0x0102,消息体属性包括消息体长度和加密方式等信息,终端手机号为注册时分配的手机号,鉴权为平台向终端下发的鉴权。 接下来,我们可以使用C语言解析JT808注册鉴权报文。我们可以定义一个结构体来存储报文的各个字段,如下所示: ```c typedef struct { uint16_t msg_id; uint16_t msg_len; uint8_t phone_num[6]; uint8_t auth_code[MAX_AUTH_CODE_LEN]; } JT808Auth; ``` 其中,msg_id和msg_len分别表示消息ID和消息体属性中的消息体长度,phone_num表示终端手机号,auth_code则用一个字符数组来存储鉴权,MAX_AUTH_CODE_LEN为鉴权的最大长度。 接着,我们可以编写解析函数,如下所示: ```c int parse_jt808_auth(uint8_t *buf, size_t buf_len, JT808Auth *auth) { if (buf_len < JT808_AUTH_MIN_LEN) { return -1; // 数据不足,解析失败 } uint8_t *p = buf; auth->msg_id = ntohs(*(uint16_t*)p); // 解析消息ID p += 2; uint16_t msg_attr = ntohs(*(uint16_t*)p); // 解析消息体属性 auth->msg_len = msg_attr & 0x3ff; // 取出消息体长度 p += 2; memcpy(auth->phone_num, p, 6); // 解析终端手机号 p += 6; memcpy(auth->auth_code, p, auth->msg_len - 6); // 解析鉴权 auth->auth_code[auth->msg_len - 6] = '\0'; // 在鉴权后面添加'\0',方便字符串操作 return 0; } ``` 在解析函数中,首先判断数据长度是否足够,如果不足则直接返回解析失败。接着,使用指针p来指向数据缓冲区,根据JT808注册鉴权报文的格式依次解析消息ID、消息体属性、终端手机号和鉴权等字段。其中,消息ID和消息体属性需要使用ntohs函数将网络字节序转换成主机字节序。最后,将鉴权后面添加一个'\0',方便后续的字符串操作。 最后,我们可以编写一个简单的测试程序来验证解析函数的正确性,如下所示: ```c int main() { uint8_t buf[] = {0x01, 0x02, 0x00, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, '1', '2', '3', '4', '5', '6'}; JT808Auth auth; int ret = parse_jt808_auth(buf, sizeof(buf), &auth); if (ret != 0) { printf("parse failed\n"); return -1; } printf("msg_id: 0x%04x\n", auth.msg_id); printf("msg_len: %d\n", auth.msg_len); printf("phone_num: %02x%02x%02x%02x%02x%02x\n", auth.phone_num[0], auth.phone_num[1], auth.phone_num[2], auth.phone_num[3], auth.phone_num[4], auth.phone_num[5]); printf("auth_code: %s\n", auth.auth_code); return 0; } ``` 在测试程序中,我们构造一个长度为10的JT808注册鉴权报文,其中鉴权为"123456"。然后调用解析函数,将解析结果打印出来,验证解析函数的正确性。 以上就是使用C语言解析JT808注册鉴权报文的方法,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值