How to clear the data in the socket receiving buffer in Linux

Source: Internet
Author: User

========================================================== ==========
This article isHeyuanhuiOriginal

Make sure that the original author information and original links of this article are complete and retained!

NN: khler
QQ: 23381103
========================================================== ==========

Recently, I encountered a problem. For socket communication in blocking mode, if you want to implement device command control, the buffer zone cannot contain information not retrieved from the previous communication before entering the command stream, otherwise, once the command is issued and then the buffer is read, it is clear that the last remaining data will be read. The practice is of course very simple, that is, to clear the buffer data in the receiving area first, but how to clear it?

There are many such problems on the Internet, but there are no standard solutions. Some even have to close the socket first to achieve the goal of clearing the socket. This is too big, it is not a reasonable solution to solve a small problem.

Another problem is to use Recv to read data. However, because you do not know the amount of data in the cache, if you use the blocking mode, you must wait until the timeout to know that the data has been read.

The other is to use fgetc to determine whether it is feof by returning it:

Whlie (1 ){
A = fgetc (f );
If (feof (F) break;
B = fgetc (f );
If (feof (F) break;

Of course, I don't know if the last call to fgetc will be blocked after the read is completed. I need to test it.

In non-blocking mode, we can easily solve this problem with Recv, but in blocking mode, we cannot directly call Recv to clear the buffer because we do not know how much data is available.

Using a small technique and using the select function, we can easily solve this problem:

The select function is used to monitor a file descriptor set. If the descriptor in the set does not change, it will be blocked until the time-out period arrives. During the time-out period, once a descriptor triggers an event of interest to you, select returns immediately and processes the event by retrieving the file descriptor set. If an error occurs in the select function, a value smaller than zero is returned. If an event is triggered, the number of descriptors of the trigger event is returned. If the time-out period is reached, 0 is returned, meaning no data is readable.

The focus is: we can use the select timeout feature to set the timeout value to 0. By checking the return value of select, we can determine whether the buffer is cleared. This technique turns a blocked socket into a 'nonblocking 'socket.

Now we can get a solution: Use the select function to monitor the socket descriptor to be cleared, and set the timeout time to 0, read one byte each time and discard it (or process it as needed). Once select returns 0, the buffer has no data ("timed out ).

Struct timeval tmout;
Tmout. TV _sec = 0;
Tmout. TV _usec = 0;
Fd_set FDS;
Fd_zeros (& FDs );
Fd_set (SKT, & FDs );

Int nret;

Char TMP [2];

Memset (TMP, 0, sizeof (TMP ));

While (1)
{Nret = select (fd_setsize, & FDS, null, null, & tmout );
If (nret = 0) break;
Recv (SKT, TMP, 1, 0 );

The advantage of this method is that you no longer need to use blocking functions such as Recv and recvfrom to directly read data. Instead, you can use select to check whether the buffer zone is empty and whether data exists, call Recv to clear data.

Some people say that the Recv and socket time-out settings can also be used for clearing. This is correct, but you need to directly set the timeout time for the socket descriptor, directly modifying the properties of the socket descriptor in order to clear data may affect the use of other places and cause strange system problems. Therefore, it is not recommended.

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: 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.