Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

绿色网站无广告
会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

通过socket接收数据并进行解析内存数据

来源: pfgmylove 分享至:

以下是由Socket接收到数据,然后再对数据进行解析。

 

ComSimuData.h

class ComSimuData
{
public:
    ComSimuData();
    ~ComSimuData();
    VOID SetCarARRouteData(COM_CarARRouteData &routeData);
 ///AR用ルート道塗情報取得
 eCOM_ERROR_CODE
 GetCarARRouteData(
  COM_CarARRouteData&     out_cararrtdt   ///AR用ルート道塗情報     【OUT】
  );
private:

    COM_CarARRouteData *m_pRouteData;
    NP_BOOL bRouteData;
};

ComSimuData.cpp

#ifndef CXX_COMSIMUDATA_H
#   include "ComSimuData.h"
#endif


ComSimuData::ComSimuData()
:bRouteData(NP_FALSE)
{
    m_pRouteData = new COM_CarARRouteData();
}

ComSimuData::~ComSimuData()
{
    if(m_pRouteData != NULL)
    {
        delete m_pRouteData;
        m_pRouteData = NULL;
    }
}

VOID
ComSimuData::SetCarARRouteData(COM_CarARRouteData &routeData)
{
    m_pRouteData->routeID = routeData.routeID;
    m_pRouteData->numOfARRtData = routeData.numOfARRtData;
    memcpy(&(m_pRouteData->carARRoute),routeData.carARRoute,sizeof(routeData.carARRoute));
}

eCOM_ERROR_CODE
ComSimuData::GetCarARRouteData(COM_CarARRouteData &out_cararrtdt)
{
    if((&out_cararrtdt) != NULL)
    {
        out_cararrtdt.routeID = m_pRouteData->routeID;
        out_cararrtdt.numOfARRtData = m_pRouteData->numOfARRtData;
        memcpy(&(out_cararrtdt.carARRoute),&(m_pRouteData->carARRoute),sizeof(m_pRouteData->carARRoute));
        return COM_ERROR_CODE_NON;
    }
    else
    {
        return COM_ERROR_CODE_INITIALIZE_FAILED;
    }

}

 

ComSimuListener使用scoket接收数据

#include "stdafx.h"

#ifndef CXX_COMSIMULISTENER_H
#   include "ComSimuListener.h"
#endif

#ifndef CXX_AL_COMMONFILE_H
#   include "AL_CommonFile.h"
#endif

#ifndef CXX_AL_LOG_H
#   include "AL_Log.h"
#endif

#ifndef CXX_COMCALLBACK_H
#   include "ComCallBack.h"
#endif

#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define COMDEBUG
#include "ComSimuData.h"
#include "COM_EventDef.h"

const char COM_HOST_IP_PORT_CONF_FILE[] = "/sdcard/Simu_Phone_Conf.xml";
const SHORT COM_HOST_PORT = 2345;
const int   COM_RECV_BUFFER_LEN = 400 * 1024;
const BYTE* s_buf = new BYTE[COM_RECV_BUFFER_LEN];
const char COM_DATA_HEAD[] = "COMDATAHEAD";
const char COM_DATA_ENDING[] = "COMDATAENDING";

ComSimuListener::ComSimuListener()
: m_bStop(NP_FALSE)
, m_iSockFD(-1)
, m_pComCallBack()
{

}

ComSimuListener::~ComSimuListener()
{
    if(-1 != m_iSockFD)
    {
        StopListen();
    }
    if(s_buf)
    {
        delete[] s_buf;
    }
}

VOID ComSimuListener::SetComCallBack(ComCallBack* pCallBack)
{
    m_pComCallBack = pCallBack;
}

NP_BOOL ComSimuListener::StartListen()
{
    m_bStop = NP_FALSE;

    DWORD ip;
    SHORT port;
    if(NP_TRUE == GetServIPPort(ip, port))
    {
        Listen(ip, COM_HOST_PORT);
    }
    return NP_TRUE;
}

VOID ComSimuListener::StopListen()
{
    OutputDebugLog("[Com] ComSimuListener->StopListen()");
    m_bStop = NP_TRUE;
    shutdown(m_iSockFD, 2);
    close(m_iSockFD);
    m_iSockFD = -1;
    OutputDebugLog("[Com] awake");
}

NP_BOOL ComSimuListener::GetServIPPort(DWORD& ip, SHORT& port)
{
    AL_CommonFile conff;
    if(NP_TRUE == conff.Open(COM_HOST_IP_PORT_CONF_FILE))
    {
        char buf[101];
        memset(&buf, 0x00, 1001);
        DWORD read = 0;
        if(NP_TRUE == conff.Read(buf, 100, (DWORD*)NULL ))
        {
            AL_String content(buf);
            int colon = content.Find(":");
            if(colon != -1)
            {
                in_addr in;
                if(inet_aton(content.Left(colon).GetString(), &in))
                {
                    ip = in.s_addr;
                }
                port = atoi(content.Right(content.GetLength() - colon - 1).GetString());
            }
        }
    }

    NP_BOOL ret = NP_TRUE;
    if(port == 0 || ip == 0)
    {
        ret = NP_FALSE;
    }

    return ret;
}

VOID ComSimuListener::Listen(DWORD ip, SHORT port)
{
    int client_fd;
 struct sockaddr_in my_addr;
 struct sockaddr_in remote_addr;
 if ((m_iSockFD = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  AL_Log::Output(AL_LOG_KIND_COM, "[Com] Create socket error");   // temp: Log id
  return;
 }
    OutputDebugLog("[Com] Client prepare to connect!");

 BOOL bReuseaddr = TRUE;
    setsockopt(m_iSockFD,SOL_SOCKET,15,(const char*)&bReuseaddr,sizeof(BOOL));

 my_addr.sin_family=AF_INET;
 my_addr.sin_port=htons(port);
 my_addr.sin_addr.s_addr = INADDR_ANY;
 bzero(&(my_addr.sin_zero),8);
 if (bind(m_iSockFD, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
  AL_Log::Output(AL_LOG_KIND_COM, "[Com] bind socket error");   // temp: Log id
  return;
 }
 if (listen(m_iSockFD, 20) == -1) {
  AL_Log::Output(AL_LOG_KIND_COM, "[Com] listen socket error");   // temp: Log id
  return;
 }

 while(1) {
     if(m_bStop)
     {
         break;
     }
  int sin_size = sizeof(struct sockaddr_in);
  if ((client_fd = accept(m_iSockFD, (struct sockaddr *)&remote_addr, (socklen_t*)&sin_size)) == -1) {
   AL_Log::Output(AL_LOG_KIND_COM, "[Com] accpet socket error");   // temp: Log id
   continue;
  }

  OutputDebugLog("[Com] Client accepted");

        while(1){
            memset((void*)s_buf, 0, COM_RECV_BUFFER_LEN);
            int recvSize = recv(client_fd, (void*)s_buf, COM_RECV_BUFFER_LEN, 0);
            OutputDebugLog("[Com] Received bytes: %d", recvSize);
            if(recvSize == -1)
            {
                AL_Log::Output(AL_LOG_KIND_COM, "[Com] recv socket error");
            }
            if(recvSize <=0 )
            {
                break;
            }

#ifdef COMDEBUG
    FILE* fp = fopen("/sdcard/comdebug", "a+");
    if(fp)
    {
        fwrite(s_buf, 1, recvSize, fp);
        fclose(fp);
    }
#endif

            OutputDebugLog((char*) s_buf);
            ProcessRecvData(recvSize);

        }

  AL_Log::Output(AL_LOG_KIND_COM, "received a connection from %s \n", inet_ntoa(remote_addr.sin_addr)); // temp: Log id

  close(client_fd);
 }
}

VOID ComSimuListener::ProcessRecvData(int recvSize)
{
    if(!s_buf || recvSize <= strlen(COM_DATA_HEAD))
    {
        return;
    }

 

    if(!IsComData())
    {
        OutputDebugLog("[COM] ComSimuListener ProcessRecvData IsComData FALSE");
        AL_Log::Output(AL_LOG_KIND_COM, "[Com] received data is no of com");
        return;
    }


    for(int offset = strlen(COM_DATA_HEAD); offset < recvSize; )
    {
        if(IsEnd(offset))
        {
            break;
        }
        int32_t length = *((int32_t*)(s_buf + offset));
        offset += 4;
        OutputDebugLog("[COM] Receive data's length is %d", length);
        sDDS_CallBackArgument arg;
        memset(&arg, 0x00, sizeof(sDDS_CallBackArgument));
        memcpy(&arg, s_buf + offset, length);

        OutputDebugLog("[COM] Arg0 : %02X",((BYTE*)(&arg))[0]);
        OutputDebugLog("[COM] Arg1 : %02X",((BYTE*)(&arg))[1]);
        OutputDebugLog("[COM] Arg2 : %02X",((BYTE*)(&arg))[2]);
        OutputDebugLog("[COM] Arg3 : %02X",((BYTE*)(&arg))[3]);
        OutputDebugLog("[COM] ComSimuListener ProcessRecvData Notify");
        int eventKind = *(int32_t*)(s_buf + offset);
        offset += 4;
        int eventID = *(int32_t*)(s_buf + offset);
        offset += 4;

        //  COM_CALL_BACK_EVENT_ID_CAR_AR_ROUTE
        if(eventID == COM_CALL_BACK_EVENT_ID_CAR_AR_ROUTE)
        {
            COM_CarARRouteData routeData;                                                  //类的结构在下面
            routeData.routeID = *(DWORD*)(s_buf + offset);
            offset += sizeof(DWORD);
            routeData.numOfARRtData = *(WORD*)(s_buf + offset);
            offset += sizeof(WORD);
            memcpy(&(routeData.carARRoute), (s_buf + offset), sizeof(sCOM_CarARRoute));
            ComSimuData simuData;
            simuData.SetCarARRouteData(routeData);
        }
        m_pComCallBack->Notify(arg);
        offset += length;

    }
}

NP_BOOL ComSimuListener::IsComData()
{
    NP_BOOL ret = NP_FALSE;
    if(s_buf)
    {
        char* pHeader = new char[strlen(COM_DATA_HEAD) + 1];
        memset(pHeader, 0, strlen(COM_DATA_HEAD)+ 1);

        memcpy(pHeader, s_buf, strlen(COM_DATA_HEAD));

        ret = strcmp(pHeader, COM_DATA_HEAD) == 0 ? NP_TRUE : NP_FALSE;
    }

    return ret;
}

NP_BOOL ComSimuListener::IsEnd(int offset)
{
    NP_BOOL ret = FALSE;
    if(s_buf)
    {
        char* pHeader = new char[strlen(COM_DATA_ENDING) + 1];
        memset(pHeader, 0, strlen(COM_DATA_ENDING)+ 1);

        memcpy(pHeader, s_buf + offset, strlen(COM_DATA_ENDING));

        ret = strcmp(pHeader, COM_DATA_ENDING) == 0 ? NP_TRUE : NP_FALSE;
    }

    return ret;
}


 /*NP_BOOL saveMapRouteData(AL_String fileName, DWORD size)
{
    AL_CommonFile file;
    NP_BOOL flag = NP_FALSE;
    if(TRUE == file.Open(fileName, AL_FILE_OPEN_MODE_A))
    {
        flag = file.Write(s_buf, size, (DWORD *)NULL);
    }
    file.Close();
    return flag;
}*/

 

类COM_CarARRouteData的结构

class COM_CarARRouteData
{
public:
          void clearData();  

public:
 DWORD routeID;                         //DWORD 是unsigned long

WORD numOfARRtData;           //WORD 是unsigned short

sCOM_CarARRoute carARRoute[ 256 ];  

};

结构体:sCOM_CarARRoute的结构

struct sCOM_CarARRoute

{
 SHORT latStart;  

 SHORT lonStart;  

 SHORT latEnd; 

 SHORT lonEnd;       //SHORT是short                       

 eCOM_CAR_ROAD_TYPE roadType;    //布尔值

 DWORD parcelID[ 2 ];   //DWORD 是unsigned long

} ;


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史