A simple SOCKET program's data packet structure and encapsulation and decapsulation functions

Publisher:等风来88888Latest update time:2015-05-18 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
A piece of code I wrote when practicing writing a socket communication program. I originally wanted to write a chat room, but the progress was stuck on the interface. The following sections are all the communication code.

Because it is just for testing, it is all written in C. After the interface is completed, it will be encapsulated with classes.
Because my E language is very poor, the names of variables and functions are Chinese-style. Please understand. But I have annotated it in detail. I would like to use this article
to commemorate my crappy self-taught programming career ......................  #include " stdio.h "
 
#include      <
windows.h  > ///      ... //Data packet sequence number    DWORD sjbcd; //Data packet length }; //Server-》Client packet struct MSG_STOC {     DWORD cmd; //Command identifier  DWORD sjbcd; //The length of the entire MSG_STOC data packet buffer  BYTE shuju[1000]; //Additional data buffer }; //Client-》Server packet struct MSG_CTOS {     DWORD cmd; //Command identifier  DWORD sjbcd; //The length of the entire MSG_CTOS data packet buffer  BYTE shuju[1800]; //Additional data buffer }; //Union  MSG_DATA {       struct MSG_STOC msg_stoc;    struct MSG_CTOS msg_ctos;     }; //Complete data packet struct SJB {  struct MSG_TOU tou; //Data packet header  union MSG_DATA data; //Data buffer }; /// ... ​ ​ ​ ​cmd,DWORD sxid,struct SJGD*sjgd,struct SJB*sjb) {  sjb->tou.lxid=lxid; //Fill the data packet type  sjb->tou.sxid=sxid; //Fill the data packet sequence number  if(lxid==CTOS)  {   sjb->data.msg_ctos.cmd=cmd; //Fill the command identifier   MoveMemory(sjb->data.msg_ctos.shuju,sjgd->sjgd,sjgd->sjcd); //Fill the additional data   sjb->data.msg_ctos.sjbcd=sjgd->sjcd; //Fill the length of the entire additional data buffer  }  if(lxid==STOC)  {   sjb->data.msg_stoc.cmd=cmd; //Fill command identifier         MoveMemory(sjb->data.msg_stoc.shuju,sjgd->sjgd,sjgd->sjcd); //Fill additional data   sjb->data.msg_stoc.sjbcd=sjgd->sjcd; //Fill the length of the entire additional data buffer  }  sjb->tou.sjbcd=20+sjgd->sjcd; //Fill data packet length  if((lxid!=CTOS)&&(lxid!=STOC)) return 0;  return 1; }[page] /// ... /// ...

































































//The function is to send a standard data packet in binary form to the data pipe buffer
int Fabao(struct SJB*sjb,struct SJGD*sjgd)
{
 MoveMemory(sjgd->sjgd,&sjb->tou.lxid,4); //Fill in data packet type
    MoveMemory(sjgd->sjgd+4,&sjb->tou.sxid,4); //Fill in data packet sequence number
 if(sjb->tou.lxid==CTOS)
 {
        MoveMemory(sjgd->sjgd+8,&sjb->tou.sjbcd,4); //Fill the length of the data packet
        MoveMemory(sjgd->sjgd+12,&sjb->data.msg_ctos.cmd,4);//Fill the command identifier
        MoveMemory(sjgd->sjgd+16,&sjb->data.msg_ctos.sjbcd,4);//Fill the length of the entire additional data buffer     
        MoveMemory(sjgd->sjgd+20,sjb->data.msg_ctos.shuju,sjb->data.msg_ctos.sjbcd); //Fill additional data
        sjgd->sjcd=sjb->tou.sjbcd; //Update the data pipeline length data
     return 1;
 }
 if(sjb->tou.lxid==STOC)
 {
        MoveMemory(sjgd->sjgd+8,&sjb->tou.sjbcd,4);//Fill the length of the data packet
        MoveMemory(sjgd->sjgd+12,&sjb->data.msg_stoc.cmd,4);//Fill the command identifier
        MoveMemory(sjgd->sjgd+16,&sjb->data.msg_stoc.sjbcd,4);//Fill the length of the entire additional data buffer
        MoveMemory(sjgd->sjgd+20,sjb->data.msg_ctos.shuju,sjb->data.msg_stoc.sjbcd); //Fill additional data
        sjgd->sjcd=sjb->tou.sjbcd; //Update the data pipeline length data
     return 1;
 }
 return 0;
}
///
...



 ​
 ​
 ​
 ​
 if((c>='0')&&(c<='9'))
 {
 return (int)(c-'0');
 }
 return 0;
}
DWORD hex_dw(BYTE*hex) //Read a 4-bit integer from memory
{
 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;
////
////////////////////////////////////////////////////////////////////////////////////
///
...The second parameter is the unpacked data storage structure
//The function is to unpack the data in the data pipeline into a standard remote control data packet to control the program flow
int Jiebao (struct SJGD*sjgd, struct SJB*sjb)
{
 sjb->tou.lxid=hex_dw(sjgd->sjgd); //Unpacked data packet type
 sjb->tou.sxid=hex_dw(sjgd->sjgd+4); //Decapsulate data packet sequence number
    sjb->tou.sjbcd=hex_dw(sjgd->sjgd+8); //Decapsulate data packet length
 if(sjb->tou.lxid==CTOS)
 {
  sjb->data.msg_ctos.cmd=hex_dw(sjgd->sjgd+12); //Decapsulate command identifier
  sjb->data.msg_ctos.sjbcd=hex_dw(sjgd->sjgd+16); //Decapsulate the length of the entire additional data buffer
  MoveMemory(sjb->data.msg_ctos.shuju,sjgd->sjgd+20,sjb->data.msg_ctos.sjbcd); //Decapsulate additional data
  return 1;
 }
 if(sjb->tou.lxid==STOC)
 {
        sjb->data.msg_stoc.cmd=hex_dw(sjgd->sjgd+12); //Unseal command identifier
  sjb->data.msg_stoc.sjbcd=hex_dw(sjgd->sjgd+16); //Unseal the length of the entire additional data buffer
  MoveMemory(sjb->data.msg_stoc.shuju,sjgd->sjgd+20,sjb->data.msg_ctos.sjbcd); //Unseal additional data
  return 1;
 }
 return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////

This is the server-side send and receive thread function http://www.51hei.com/mcu/2611.html

Reference address:A simple SOCKET program's data packet structure and encapsulation and decapsulation functions

Previous article:Screen snow program implemented by linked list
Next article:The lossless compression ratio of LZW compression is as high as 4:1.

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号