Cyclic buffer solves incomplete data reception in Network Communication

Source: Internet
Author: User

In network communication, data acquisition is sometimes not based on your ideas. Every time you get a complete data frame, you get a broken frame, A complete frame can be obtained in several segments, or several complete frames can be obtained at a time. How to obtain the complete frame to be processed and use the cyclic buffer can solve this problem.


[Csharp]
// Cyclic buffer for receiving and processing data
Byte [] receiveBuffer = new Byte [16]; // data receiving slowdown
Byte [] cycleBuffer = new Byte [1024]; // used to save data
Byte [] frameBuffer = new Byte [16]; // The frame obtained from cycleBuffer
Int32 receiveCount; // number of received data
Int32 indexHead, indexTail; // header pointer, tail pointer
IndexHead = indexTail = 0;

// Cyclic buffer for receiving and processing data
Byte [] receiveBuffer = new Byte [16]; // data receiving slowdown
Byte [] cycleBuffer = new Byte [1024]; // used to save data
Byte [] frameBuffer = new Byte [16]; // The frame obtained from cycleBuffer
Int32 receiveCount; // number of received data
Int32 indexHead, indexTail; // header pointer, tail pointer
IndexHead = indexTail = 0;
[Csharp]
// Put the received data in the cyclic buffer.
For (int I = 0; I <receiveCount; I ++)
{
IndexTail ++;
If (indexTail = cycleBuffer. Length)
{
IndexTail = 0;
}
CycleBuffer [indexTail] = receiveBuffer [I];
}

// Put the received data in the cyclic buffer.
For (int I = 0; I <receiveCount; I ++)
{
IndexTail ++;
If (indexTail = cycleBuffer. Length)
{
IndexTail = 0;
}
CycleBuffer [indexTail] = receiveBuffer [I];
}
[Csharp]
// Start to get the frame in the loop buffer-specify the string
Int32 indexTemp = indexHead;
While (indexTemp! = IndexTail)
{
If (cycleBuffer [indexTemp] = (Byte) '$') // locate the frame
{
Int32 dec = 0;
While (indexHead! = IndexTemp)
{
FrameBuffer [dec ++] = cycleBuffer [indexHead];
IndexHead ++;
If (indexHead = cycleBuffer. Length)
{
IndexHead = 0;
}
}
 
// Process the retrieved frame of data
........................
}
Else
{
IndexTemp ++;
If (indexTemp = cycleBuffer. Length)
{
IndexTemp = 0;
}
}
}

// Start to get the frame in the loop buffer-specify the string
Int32 indexTemp = indexHead;
While (indexTemp! = IndexTail)
{
If (cycleBuffer [indexTemp] = (Byte) '$') // locate the frame
{
Int32 dec = 0;
While (indexHead! = IndexTemp)
{
FrameBuffer [dec ++] = cycleBuffer [indexHead];
IndexHead ++;
If (indexHead = cycleBuffer. Length)
{
IndexHead = 0;
}
}

// Process the retrieved frame of data
........................
}
Else
{
IndexTemp ++;
If (indexTemp = cycleBuffer. Length)
{
IndexTemp = 0;
}
}
}

 

 

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.