Data Logger Service--socket

Source: Internet
Author: User

Recently just done a project about Socket TCP communication.

The demand side provides an ARM machine, and a data logger, which requires me to do a service-side interaction with the data collector.


Objective:

Data logger: Periodically sends the readings to the server.

Server: The data sent by the Datalogger is saved locally.


Requirements:

1. Communication is interactive by TCP and the port can be configured.

2, self-realization handshake, heartbeat packet mechanism.

3, TCP packet structure includes: Baotou, the total length of effective data, valid data, CRC check, packet tail, which valid data including instruction sequence number and instruction content, for the AES 128-bit encrypted XML data.

4, encryption algorithm: AES 128-bit, encryption mode: CBC, fill mode: Nopadding.

5, handshake Process: (The following process is transmitted in the TCP packet structure encryption, and only to mention the main content data, omitting the CRC check description)

(1) The Data collector sends the request packet to the service side, and the service side resolves the packet structure and extracts the segment for AES decryption.

(2) The server is judged by the packet as the request packet and replies to a packet containing a random sequence to the data collector.

(3) After the packet is received by the data collector, the random sequence is combined with the MD5 key and then MD5 encrypted and sent back to the server.

(4) The service side receives the encrypted content, and compares with the local encrypted content, passes the reply pass, otherwise restores the fail.

(5) The data collector receives the pass and the handshake succeeds and establishes the connection, otherwise the handshake fails and the connection is disconnected.

6. Heartbeat pack: After the handshake is established, the collector sends a heartbeat packet to the server at a certain time, and the service receives the heartbeat packet and responds immediately, replying to the current time of the information for the collector.

7. Packet: After the handshake is established, the data packet will send the breakpoint packet and the real-time packet to the server, and the server will save the data to the local PC and reply the pass.


1. Create a service-side socket

(C #)

IPEndPoint Ipep = new IPEndPoint (Ipaddress.any, 36829); Native pre-used IP and port socket Newsock = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp); Newsock. Bind (IPEP); Bind Newsock. Listen (10); Listening

(node. js)

var server = Net.createserver (socket) {//' connection ' Listenerconsole.log (' client Connected ');}); Server.listen (36829, function () {//' listening ' listenerconsole.log (' server Bound ');});

2. Create Client objects and listen

(C #)

Console.WriteLine ("Waiting for a Client"); Socket client = Newsock. Accept (); When a client connection attempt is available, it executes and returns a new socket for communication with the client IPEndPoint ClientIP = (ipendpoint) client. Remoteendpoint; Console.WriteLine ("Connect with Client:" + ClientIP. Address + "at Port:" + ClientIP. Port), while (true) {byte[] buffer = new byte[1024];//For caching the information sent by the client, the information passed through the socket must be a byte array int packagelength = client. Receive (buffer); Used to indicate the length of information sent by the client if (Packagelength = = 0)    {        Console.WriteLine ("De-looping listener, disconnected from" + ClientIP. Address);        Break When the message length is 0, the client connection is broken    }}

(node. js)

var server = Net.createserver (socket) {//' connection ' Listenerconsole.log (' client connected '); Socket.on (' End ', function () {Console.log (' client disconnected ');}); Socket.on (' Data ', function (data) {Console.log (data.length); Console.log (data);});}); Server.listen (36829, function () {//' listening ' listenerconsole.log (' server Bound ');});

AES Decrypt public static string Aesdecrypt (byte[] data) {symmetricalgorithm AES = Rijndael.create (); Aes.    Key = Keyarray;    AES.IV = Keyarray; Aes.    Mode = CIPHERMODE.CBC; Aes.    Padding = Paddingmode.none; byte[] decryptbytes = new Byte[data.    Length]; ICryptoTransform ctransform = AES. CreateDecryptor (AES.    Key, AES.IV); Decryptbytes = ctransform.transformfinalblock (data, 0, data.    Length); using (MemoryStream ms = new MemoryStream (data)) {using (CryptoStream cs = new CryptoStream (MS, AES). CreateDecryptor (), CryptoStreamMode.Read)) {cs. Read (decryptbytes, 0, decryptbytes.length); <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" > cs. Close (); <pre name= "code" class= "CSharp" ><pRe name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre Name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre Name= "code" class= "CSharp" > Ms.        Close (); }} AES.    Clear (); Return System.Text.Encoding.Default.GetString (decryptbytes). Replace ("n", "");}


AES encryption public static byte[] Aesencrypt (byte[] data) {symmetricalgorithm AES = Rijndael.create (); <pre name= "code" c lass= "CSharp" > AES. Key = keyarray;<pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" > Aes.iv = Keyarray;<pre n Ame= "code" class= "CSharp" ><pre name= "code" class= "CSharp" > AES. Mode = ciphermode.cbc;<pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" > AES.    Padding = Paddingmode.none; using (MemoryStream ms = new MemoryStream ()) {using (CryptoStream cs = new CryptoStream (MS, AES). CreateEncryptor (), cryptostreammode.write)) {cs. Write (data, 0, data. Length); <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= " CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "> CS.flushfinalblock (); <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" > byte[] Cipherbyt Es = Ms. ToArray (); Get encrypted byte array <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= " CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "> cs. Close (); <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= " CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "CLAss= "CSharp" > Ms. Close (); <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= " CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "> AES. Clear (); <pre name= "code" class= "CSharp" ><pre name= "code" class= "CSharp" ><pre name= "code" class= " CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class="        CSharp "><pre name=" code "class=" CSharp "><pre name=" code "class=" CSharp "> Return cipherbytes; }    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data Logger Service--socket

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.