Data upload Project Summary

Source: Internet
Author: User
Tags 0xc0

The first project ended quickly and gained a lot. What impressed me most is the importance of demand analysis. The purpose of this analysis isProgramMembers know what they want to do. When analyzing requirements, they must fully consider the difficulties in the project and negotiate with the other party to solve the problem. Otherwise, you will be miserable when coding is done, A good requirement analysis can make programming smooth, at least not too difficult. The biggest problem encountered in this project was that the historical data boundary was stuck. The main reason was that we did not negotiate well and finally got annoyed...

After the project is completed, the program will be handed over to the other party for debugging because the construction site is somewhere else... As a result, the other party has been wasted a lot of time by the small problem card owner several times. It is strange that we did not write the development documentation...

The transfer protocol for this project was written in accordance with the "technical guidelines for the Data Monitoring System of the renewable energy construction application demonstration project". It is said that it was written by experts but it feels a bit awkward.

The following summarizes the technologies used in encoding.

1. Packet group package

2. AES Encryption

3. MD5 Verification

4. cyclic redundancy check)

5. How to convert an XML file to a byte stream: Use the xmlserializer class

6. Lock for Thread Synchronization

7. Two Ways for threads to access controls

 

 

1. Packet group package

 

 

 

To store a group package, you must first create an enumerated data type to determine the data storage location.

 

View code

 
// Public Enum packageposition: int {packagehead = 0, // 0x68 0x68 0x16 0x16 16 // 4 bytes effectivedatalength = 4, // 4-byte encryption tivedata = 8 // m + 4 bytes, M is the XML encrypted length, 4 bytes is the instruction serial number}

 

In this case, it is easier to enter more data packets. You can convert an integer to a byte.

 

View code

Public byte [] effectivepack (INT instruct_seq, byte [] data) {byte [] effectivedata = new byte [4 + data. length]; inclutivedata [(INT) encodingtable. inclutivedataposition. instruction_sequence] = (byte) (instruct_seq> 24); // The 8-bit effectivedata [(INT) encodingtable. inclutivedataposition. instruction_sequence + 1] = (byte) (instruct_seq> 16) & 0xff); inclutivedata [(INT) encodingtable. inclutivedataposition. instruction_sequence + 2] = (byte) (instruct_seq> 8) & 0xff); inclutivedata [(INT) encodingtable. inclutivedataposition. instruction_sequence + 3] = (byte) (instruct_seq & 0xff); array. copy (data, 0, partition tivedata, (INT) encodingtable. inclutivedataposition. instruction_data, Data. length); Return inclutivedata ;}

 

The reverse process of unpacking is similar.

 

2. AES Encryption

C # A Lot Of encryption is provided.AlgorithmThank you for copying your copy...

Advanced Encryption Standard (AES), also known as Rijndael encryption

Advanced Encryption Standard (AES) has become one of the most popular algorithms in symmetric key encryption.

View code

Public class AES {private byte [] _ vkey; private byte [] _ key; Public AES (byte [] vkey, byte [] Key) {_ vkey = vkey; _ key = key;} public byte [] _ vkey {set {_ vkey = value;} public byte [] _ key {set {_ key = value ;}} /// <summary> /// encryption /// </Summary> /// <Param name = "data"> </param> /// <returns> </ returns> Public byte [] aesencrypt (byte [] data) {incluricalgorithm des = Rijndael. create (); des. key = _ key; des. IV = _ vkey; memorystream MS = new memorystream (); cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. write); CS. write (data, 0, Data. length); CS. flushfinalblock (); byte [] cipherbytes = Ms. toarray (); CS. close (); Ms. close (); Return cipherbytes ;} /// <summary> /// decrypt /// </Summary> /// <Param name = "data"> </param> /// <returns> </ returns> Public byte [] aesdecrypt (byte [] data) {byte [] decdata; try {export ricalgorithm des = Rijndael. create (); des. key = _ key; des. IV = _ vkey; memorystream MS = new memorystream (data); cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. read); decdata = new byte [data. length]; int length = cs. read (decdata, 0, Data. length); byte [] decdata1 = new byte [length]; array. copy (decdata, 0, decdata1, 0, length); CS. close (); Ms. close (); Return decdata1;} catch (exception ex) {system. diagnostics. trace. write ("aesdecrypt error:" + ex. message) ;}return null ;}}

 

3. MD5 Verification

The project is used to calculate the MD5 value and send it to the other party for verification.

System must be added for use. Web reference.

 

View code

 
Class md5encrypt {public static string MD5 = ""; public static string encryptmd5 (string SEQ) {string combine = seq + MD5; string enseq = system. web. security. formsauthentication. hashpasswordforstoringinconfigfile (combine, "MD5"); Return enseq ;}}

 

4. cyclic redundancy check crc16 check

It is the most common error verification code in the field of data communication, and its feature is that the length of information fields and verification fields can be arbitrarily selected.

 

View code

Public class crc16 {public crc16 () {} private readonly byte [] _ auchcrchi = new byte [] // CRC high table {0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40 }; private readonly byte [] _ auchcrclo = new byte [] // CRC low position table {0x00, 0xc0, 0xc1, 0x01, 0xc3, 0x03, 0x02, 0xc2, 0xc6, 0x06, 0x07, 0xc7, 0x05, 0xc5, 0xc4, 0x04, 0xcc, 0x0c, 0x0d, 0xcd, 0x0f, 0xcf, 0xce, 0x0e, 0x0a, 0xca, 0xcb, 0x0b, 0xc9, 0x09, 0x08, 0xc8, 0xd8, 0x18, 0x19, 0xd9, 0x1b, 0xdb, 0xda, 0x1a, 0x1e, 0xde, 0xdf, 0x1f, 0xdd, 0x1d, 0x1c, 0xdc, 0x14, 0xd4, 0xd5, 0x15, 0xd7, 0x17, 0x16, 0xd6, 0xd2, 0x12, 0x13, 0xd3, 0x11, 0xd1, 0xd0, 0x10, 0xf0, 0x30, 0x31, 0xf1, 0x33, 0xf3, 0xf2, 0x32, 0x36, 0xf6, 0xf7, 0x37, 0xf5, 0x35, 0x34, 0xf4, 0x3c, 0xfc, 0xfd, 0x3d, 0xff, 0x3f, 0x3e, 0xfe, 0xfa, 0x3a, 0x3b, 0xfb, 0x39, 0xf9, 0xf8, 0x38, 0x28, 0xe8, 0xe9, 0x29, 0xeb, 0x2b, 0x2a, 0xea, 0xEE, 0x2e, 0x2f, 0xef, 0x2d, 0xed, 0xec, 0x2c, 0xe4, 0x24, 0x25, 0xe5, 0x27, 0xe7, 0xe6, 0x26, 0x22, 0xe2, 0xe3, 0x23, 0xe1, 0x21, 0x20, 0xe0, 0xa0, 0x60, 0x61, 0xa1, 0x63, 0xa3, 0xa2, 0x62, 0x66, 0xa6, 0xa7, 0x67, 0xa5, 0x65, 0x64, 0xa4, 0x6c, 0xac, 0xad, 0x6d, 0xaf, 0x6f, 0x6e, 0xae, 0xaa, 0x6a, 0x6b, 0xab, 0x69, 0xa9, 0xa8, 0x68, 0x78, 0xb8, 0xb9, 0x79, 0xbb, 0x7b, 0x7a, 0xba, 0xbe, 0x7e, 0x7f, 0xbf, 0x7d, 0xbd, 0xbc, 0x7c, 0xb4, 0x74, 0x75, 0xb5, 0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71, 0x70, 0xb0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9c, 0x5c, 0x5d, 0x9d, 0x5f, 0x9f, 0x9e, 0x5e, 0x5a, 0x9a, 0x9b, 0x5b, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4b, 0x8b, 0x8a, 0x4a, 0x4e, 0x8e, 0x8f, 0x4f, 0x8d, 0x4d, 0x4c, 0x8c, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40}; Public ushort calculatecrc16 (byte [] buffer) {byte crchi = 0xff; // High-Level Initialization byte crclo = 0xff; // Low-Level Initialization for (INT I = 0; I <buffer. length; I ++) {int crcindex = crchi ^ buffer [I]; // query CRC Table value crchi = (byte) (crclo ^ _ auchcrchi [crcindex]); crclo = _ auchcrclo [crcindex];} return (ushort) (crchi <8 | crclo );}}

 

5. How to convert an XML file to a byte stream: Use the xmlserializer class

 

View code

Class xmloperator {// <summary> // change the byte stream to the xmldocument type /// </Summary> /// <Param name = "data"> </param>/ // <returns> </returns> Public static xmldocument bytetoxmldoc (byte [] data) {memorystream dstream = new memorystream (data); xmlserializer SER = new xmlserializer (typeof (xmldocument); xmldocument xmldata = (xmldocument) Ser. deserialize (dstream); dstream. flush (); dstream. close (); // close the memory stream and release the resource return xmldata ;} /// <summary> // convert xmldocument to a byte stream // </Summary> /// <Param name = "xmldata"> </param> // <returns> </returns> Public static byte [] xmldoctobyte (xmldocument xmldata) {xmlserializer SER = new xmlserializer (typeof (xmldocument); memorystream stream = new memorystream (); Ser. serialize (stream, xmldata); byte [] DATA = stream. getbuffer (); return data ;}}

 

6. Lock for Thread Synchronization

The lock keyword can be used to ensureCodeThe block runs completely without being interrupted by other threads. This is achieved by obtaining mutex locks for a given object during the code block operation. The lock statement starts with the keyword lock. It has an object as a parameter, and there is a code block that can only be executed by one thread at a time after this parameter. For example:

View code

 
Public class testthreading {private system. Object lockthis = new system. Object (); Public void function () {lock (lockthis) {// access thread-sensitive resources .}}}

 

The parameter provided to the lock keyword must be an object of the reference type, which is used to define the lock range. In the above example, the lock range is limited to this function because no reference to this object exists outside the function. If such a reference exists, the lock range is extended to this object. Strictly speaking, the object provided to lock is only used to uniquely identify resources shared by multiple threads, so it can be any class instance. However, in fact, this object usually indicates the resources that require thread synchronization. For example, if a container object is used by multiple threads, the container can be passed to lock, and the synchronization code block after lock will access the container. As long as other threads lock the container before accessing the container, access to the object will be synchronized securely.

Generally, it is better to avoid locking the public type or locking an object instance that is not controlled by the application. For example, if the instance can be publicly accessed, lock (this) may be faulty because uncontrolled Code may also lock the object. This may cause a deadlock, that is, two or more threads are waiting to release the same object. For the same reason, locking public data types (compared to objects) may also cause problems. Locking a string is especially dangerous because the string is "Temporarily" by the Common Language Runtime Library (CLR ". This means that there is only one instance for any given string in the entire program, and the same object represents the text in all threads in all running application domains. Therefore, as long as a lock is placed on a string with the same content anywhere in the application process, all instances of the string in the application will be locked. Therefore, it is best to lock private or protected members that are not temporarily retained. Some classes provide members dedicated for locking. For example, the array type provides syncroot. Many Collection types also provide syncroot.

 

7. Two Ways for threads to access controls

Method 1:

 
Control. checkforillegalcrossthreadcils = false;

It is said that it is not good to check whether there is a problem with cross-thread access. I am using the second method.

Method 2:

Use Delegation

 

View code

    Public   Delegate   Void Showmsg ( String  SMG );  Public   Void Showmsg ( String  MSG ){  If  (Textbox_show.invokerequired) {showmsg SW = New  Showmsg (showmsg );  This  . Invoke (SW, MSG );}  Else Textbox_show.text + = Datetime. Now. tostring () + "    " + MSG + "  \ R \ n  "  ;}  Void Client_onmessage ( Object  Sender, messageargs ARGs) {showmsg (ARGs. eventmsg );} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.