Socket solves the problem of sticking packets. 1. socket solves the problem of sticking packets.

Source: Internet
Author: User

Socket solves the problem of sticking packets. 1. socket solves the problem of sticking packets.

A sticky Package refers to the phenomenon that the sender sends packets too quickly and sends multiple packets to the receiver end to form a package. For example, the sender sends one character 'a' for 10 consecutive times ', because the sending speed is very fast, the receiving end may receive 10 characters 'aaaaaaaaaaa' at a time. This is the sticking packet of the receiving end.

It is possible that we do not feel the danger of sticking packets during normal exercises, or solve the problem of sticking packets by slowing down the sending speed. However, in real-time communication, the sending end is often an Information Collector of a single-chip microcomputer or other systems, and their transmission rate cannot be controlled. If the problem of sticking packets to the receiving end is not solved, we cannot obtain normal information.

In my own project, the receiver is a single-frequency indicator measuring instrument. It sends the information of the current single-frequency indicator to the server through the socket interface, and the packet length is about 100 bytes, one second can send about 10 packets. If I do not process the sticky packets in real time, the collected single-frequency indicators cannot be displayed in real time for the front-end.

The communication shown below is some code added based on the previous framework. First, create a PackageBuilder class for generating a package simulation class, which can automatically generate packages of different lengths for testing and sending, the Code is as follows. The code is not described because it has nothing to do with communication.

Public static class PackageBuilder {static StringBuilder package = new StringBuilder (); public static string [] BuildPackage (int count) {Random random = new Random (); int dataLength; string [] packageArr = new string [count]; int myChar; for (int j = 0; j <count; j ++) {dataLength = random. next (25) + 25; package. append ("HEAD | H1 |" + dataLength); package. append ("data:"); for (int I = 0; I <dataLength-5; I ++) {myChar = random. next (26) + 65; package. append (char) myChar);} packageArr [j] = package. toString (); package. clear () ;}return packageArr ;}View Code

Test the generated package in the main function.

Static void Main (string [] args) {string [] str = PackageBuilder. buildPackage (20); for (int I = 0; I <20; I ++) {// str = PackageBuilder. buildPackage (); Console. writeLine (str [I]); // Thread. sleep (500);} Console. read ();}View Code

There are multiple methods to solve the problem of sticking packets. Based on the structure of the sending package, I adopted the method of subcontracting Based on the package data length.

As shown in, the structure of the sending package is: packet header + Data Length + data, packet header (HEAD | H1 |) is a fixed length, the content remains unchanged, and the Data Length (two bytes) the data (data: + random characters) is also changed. All the content after the number is data, including 'data :'.

The prelude finishes the next article and returns to socket.

Related Article

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.