專注電子技術(shù)學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

一個簡單的SOCKET程序的數(shù)據(jù)包結(jié)構(gòu)和封解包函數(shù)

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年12月17日   【字體:

練習寫套接字通信程序時候?qū)懙囊欢未a,本來想寫個聊天室但寫來寫去進度卡在界面上接節(jié)下來都是通信部分的代碼
因為只是試驗用所以都是用C寫的,等界面部分完工后會用類來封裝一下
因為本人E文很爛所以變量和函數(shù)的命名是具有中國特色的,求理解.不過我注釋的很詳細了
謹以此文紀念我那坑爹的編程自學生涯......................
 
#include "stdio.h"
#include <windows.h>
//////////////////////////////數(shù)據(jù)包接構(gòu)//////////////////////////////////////
//數(shù)據(jù)包類型CTOS為客戶端使用的數(shù)據(jù)包,STOC為服務端使用的數(shù)據(jù)包
#define CTOS 1
#define STOC 2
//數(shù)據(jù)包存儲管道每個包最大為2000字節(jié),其中數(shù)據(jù)載荷為最大1800字節(jié),其余留著擴展數(shù)據(jù)包頭
struct SJGD {
 BYTE sjgd[2000];  //數(shù)據(jù)緩沖區(qū)
 DWORD sjcd;    //數(shù)據(jù)長度
};
//數(shù)據(jù)包頭大小為8字節(jié)
struct MSG_TOU {
     DWORD lxid; //數(shù)據(jù)包類型
     DWORD sxid;  //數(shù)據(jù)包順序標號
  DWORD sjbcd; //數(shù)據(jù)包長度
};
//服務端-》客戶端包
struct MSG_STOC {
    DWORD cmd;    //命令標識
 DWORD sjbcd; //整個MSG_STOC數(shù)據(jù)包緩沖區(qū)的長度
 BYTE shuju[1000];   //附加數(shù)據(jù)緩沖區(qū)
};
//客戶端-》服務器包
struct MSG_CTOS {
    DWORD cmd;    //命令標識
 DWORD sjbcd; //整個MSG_CTOS數(shù)據(jù)包緩沖區(qū)的長度
 BYTE shuju[1800]; //附加數(shù)據(jù)緩沖區(qū)
};
//共用體
 union MSG_DATA {
      struct MSG_STOC msg_stoc;
   struct MSG_CTOS msg_ctos;
    };
//完整數(shù)據(jù)包
struct SJB {
 struct MSG_TOU tou; //數(shù)據(jù)包頭
 union MSG_DATA data;  //數(shù)據(jù)緩沖區(qū)
};
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//封包函數(shù)第一個參數(shù)為數(shù)據(jù)包類型,第二個為命令標識,這是數(shù)據(jù)包的重點遠控命令會轉(zhuǎn)換成數(shù)字在這里傳輸
//第三個為數(shù)據(jù)包順序標號,第四個參數(shù)為附加數(shù)據(jù)緩沖區(qū),第五個參數(shù)為打包數(shù)據(jù)緩沖區(qū)
//作用是把數(shù)據(jù)填充成一個標準的遠控數(shù)據(jù)包為下面的發(fā)包做準備
int Fengbao(DWORD lxid,DWORD cmd,DWORD sxid,struct SJGD*sjgd,struct SJB*sjb)
{
 sjb->tou.lxid=lxid;  //填充數(shù)據(jù)包類型
 sjb->tou.sxid=sxid;  //填充數(shù)據(jù)包順序標號
 if(lxid==CTOS)
 {
  sjb->data.msg_ctos.cmd=cmd;  //填充命令標識
  MoveMemory(sjb->data.msg_ctos.shuju,sjgd->sjgd,sjgd->sjcd);  //填充附加數(shù)據(jù)
  sjb->data.msg_ctos.sjbcd=sjgd->sjcd;  //填充整個附加數(shù)據(jù)緩沖區(qū)的長度
 }
 if(lxid==STOC)
 {
  sjb->data.msg_stoc.cmd=cmd; //填充命令標識
        MoveMemory(sjb->data.msg_stoc.shuju,sjgd->sjgd,sjgd->sjcd);  //填充附加數(shù)據(jù)
  sjb->data.msg_stoc.sjbcd=sjgd->sjcd;  //填充整個附加數(shù)據(jù)緩沖區(qū)的長度
 }
 sjb->tou.sjbcd=20+sjgd->sjcd;   //填充數(shù)據(jù)包長度
 if((lxid!=CTOS)&&(lxid!=STOC)) return 0;
 return 1;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//發(fā)包函數(shù)第一個參數(shù)為待發(fā)數(shù)據(jù)包,第二個參數(shù)為數(shù)據(jù)管道緩沖區(qū)
//作用是把一個標準數(shù)據(jù)包以二進制的形式發(fā)送到數(shù)據(jù)管道緩沖區(qū)
int Fabao(struct SJB*sjb,struct SJGD*sjgd)
{
 MoveMemory(sjgd->sjgd,&sjb->tou.lxid,4); //填充數(shù)據(jù)包類型
    MoveMemory(sjgd->sjgd+4,&sjb->tou.sxid,4);//填充數(shù)據(jù)包順序標號
 if(sjb->tou.lxid==CTOS)
 {
        MoveMemory(sjgd->sjgd+8,&sjb->tou.sjbcd,4); //填充數(shù)據(jù)包長度
        MoveMemory(sjgd->sjgd+12,&sjb->data.msg_ctos.cmd,4);//填充命令標識
        MoveMemory(sjgd->sjgd+16,&sjb->data.msg_ctos.sjbcd,4);//填充整個附加數(shù)據(jù)緩沖區(qū)的長度    
        MoveMemory(sjgd->sjgd+20,sjb->data.msg_ctos.shuju,sjb->data.msg_ctos.sjbcd); //填充附加數(shù)據(jù)
        sjgd->sjcd=sjb->tou.sjbcd; //更新數(shù)據(jù)管道長度數(shù)據(jù)
     return 1;
 }
 if(sjb->tou.lxid==STOC)
 {
        MoveMemory(sjgd->sjgd+8,&sjb->tou.sjbcd,4);//填充數(shù)據(jù)包長度
        MoveMemory(sjgd->sjgd+12,&sjb->data.msg_stoc.cmd,4);//填充命令標識
        MoveMemory(sjgd->sjgd+16,&sjb->data.msg_stoc.sjbcd,4);//填充整個附加數(shù)據(jù)緩沖區(qū)的長度
        MoveMemory(sjgd->sjgd+20,sjb->data.msg_ctos.shuju,sjb->data.msg_stoc.sjbcd); //填充附加數(shù)據(jù)
        sjgd->sjcd=sjb->tou.sjbcd; //更新數(shù)據(jù)管道長度數(shù)據(jù)
     return 1;
 }
 return 0;
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//函數(shù)作用是直接從內(nèi)存讀取一個4字節(jié)的整數(shù),為下面的解包做準備
int hex_int(char c)    //從內(nèi)存讀取一個1字節(jié)的整數(shù)
{
 if((c>='A')&&(c<='F'))
 {
 return (int)(c-'A'+10);
 }
 if((c>='0')&&(c<='9'))
 {
 return (int)(c-'0');
 }
 return 0;
}
DWORD hex_dw(BYTE*hex)   //從內(nèi)存讀取一個4節(jié)的整數(shù)
{
 DWORD D=0;
    char x[2];
    sprintf(x,"%.2X",hex[0]);
 D+=hex_int(x[1]);
 D+=hex_int(x[0])*16;
    sprintf(x,"%.2X",hex[1]);
 D+=hex_int(x[1])*16*16;
 D+=hex_int(x[0])*16*16*16;
    sprintf(x,"%.2X",hex[2]);
 D+=hex_int(x[1])*16*16*16*16;
 D+=hex_int(x[0])*16*16*16*16*16;
    sprintf(x,"%.2X",hex[3]);
 D+=hex_int(x[1])*16*16*16*16*16*16;
 D+=hex_int(x[0])*16*16*16*16*16*16*16;
 return D;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//解包函數(shù)第一個參數(shù)是待解包的數(shù)據(jù)管道緩沖區(qū),第二個參數(shù)為解包數(shù)據(jù)存儲結(jié)構(gòu)
//函數(shù)作用是把數(shù)據(jù)管道中的數(shù)據(jù)解封為一個標準的遠控數(shù)據(jù)包,用以控制程序流程
int Jiebao(struct SJGD*sjgd,struct SJB*sjb)
{
 sjb->tou.lxid=hex_dw(sjgd->sjgd);   //解封數(shù)據(jù)包類型
 sjb->tou.sxid=hex_dw(sjgd->sjgd+4);  //解封數(shù)據(jù)包順序標號
    sjb->tou.sjbcd=hex_dw(sjgd->sjgd+8); //解封數(shù)據(jù)包長度
 if(sjb->tou.lxid==CTOS)
 {
  sjb->data.msg_ctos.cmd=hex_dw(sjgd->sjgd+12);  //解封命令標識
  sjb->data.msg_ctos.sjbcd=hex_dw(sjgd->sjgd+16); //解封整個附加數(shù)據(jù)緩沖區(qū)的長度
  MoveMemory(sjb->data.msg_ctos.shuju,sjgd->sjgd+20,sjb->data.msg_ctos.sjbcd); //解封附加數(shù)據(jù)
  return 1;
 }
 if(sjb->tou.lxid==STOC)
 {
        sjb->data.msg_stoc.cmd=hex_dw(sjgd->sjgd+12);  //解封命令標識
  sjb->data.msg_stoc.sjbcd=hex_dw(sjgd->sjgd+16); //解封整個附加數(shù)據(jù)緩沖區(qū)的長度
  MoveMemory(sjb->data.msg_stoc.shuju,sjgd->sjgd+20,sjb->data.msg_ctos.sjbcd); //解封附加數(shù)據(jù)
  return 1;
 }
 return 0;
}
////////////////////////////////////////////////////////////////////////////////////////

這篇是服務器端的收發(fā)線程函數(shù) http://www.torrancerestoration.com/mcu/2611.html

關(guān)閉窗口

相關(guān)文章