When writing the Socket communication program, in order to verify the integrity of the transmitted packet, I usually use it to the packet additional check code.
The method, the specific approach is: the data to be transmitted from the first character to the last character, and finally get a different or
Result characters, convert this character to a 2-way string! Attach it later, charge data, after receiving data, first defined
The format splits the packet, which is different or the received data, and then compares the result and the sender's check code, if consistent, the data transmission is explained.
no problem.
An example in the practical application of a bank interface:
// definition of message structure typedef struct tradePackage {char p_trade_code [4]; char p_mobile_no [16]; char p_agent_account [26]; char p_our_account [26]; char p_trade_fee [16]; char p_trade_date [9]; char p_trade_time [7 ]; Char p_bank_not [3]; char p_trade_node [11]; char p_operator_no [5]; char p_bank_flow_no [13]; char p_Result_code [4]; char p_or_flow_no [13]; char p_check_code [9];
/ * Function Description: Generate the check code of sending packets (displayed as 2-based character mode) Returns: checkcode Different or generated check code for each character of the Input_BUF string * / void make_xor_Checkcode (char * Input_buf, char * checkcode) {int m = strlen; INT i, n; // save distant results Char Ret; char s [M]; char b [8]; int x = 0x80; strcpy S, INPUT_BUF); // Different or for (i = 0; i
/ * Function Description: Whether the content of the data packets issued is consistent with the check code: trade_package parameter is a message from the bank, with trade_package.p_check_code to check for other strings.
The calibration successfully returns 1, otherwise returns 0 * / int package_page_parse (STRUCT TRADEPAGE TRADE_PACKAGE) {// Packet format: Trading code 3 mobile phone number 15 agent bank account 25 Mechanian bank account 25 amount 15 Trading Date 8 Trading time 6 Bank Code 2 Trading Network 10 // Operator 4 Bank Water No. 12 Results Code 3 Merchants Water No. 12 Check Code 8 Char CHKCODE [8]; Char StrtMP [256]; Char Input_STR [1024]; char Checkcode [8]; MEMSET INPUT_STR, 0X0, SIZEOF (Input_Str)); // 1: Traffic code 3 is not empty STRCPY (INPUT_STR, Trade_Package.p_trade_code); // 2: Mobile phone number 15 is not empty right tonic space MEMSET (strs, 0x0, 15); Sprintf (strtmp, "% - 15s", trade_package.p_mobile_no); // Display 15 bits (instead of filling) when the string is displayed, aligned against the left end (Input_Str, StrtMP); // 3: Agent Bank Account 25 Do not allow free norterate spaces (such as cash payment, full space) MEMSET (strintf, 0x0, 25); sprintf (startmp, "% - 25s", trade_package.p_agent_account); // Show string to limit 25 bits (insufficiency Right-tensioned spaces and aligned with left ends STRCAT (Input_Str, StrtMP); // 4: Merchant Bank Account 25 is not empty MEMSET (StrtMP, 0x0, 25); Sprintf (startMP, "% - 25s", trade_package.p_our_account; // Displaying a string limited to 25 bits (inexpensive, right-added spaces), align Strcat on the left end (INPUT_STR, STRTMP); // 5: The amount 15 is not Empty decimal point and two decimals, right-tonic space //sprintf (TRADE_PACKAGE.P_TRADE_FEE, "%0.2F" ,p_trade_fee); MEMSET (strs); sprintf (strintf, "% - 15s", trade_package.p_trade_fee ); // Show strings to limit 15 bits (insufficient, right-added spaces), align Strcat (input_str, strtmp) on the left end (INPUT_STR, STRTMP); // 6: Trading Date 8 is not empty YYYYMMDD STRCAT (INPUT_STR, TRADE_PACKAGE.P_TRADE_DATE); / / 7: Trading time 6 is not empty hHMMSS STRCAT (INPUT_STR, Trade_Package.p_trade_time);