How to clear the socket buffer in Linux

Source: Internet
Author: User

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?

The socket does not receive data that way.
Because the socket sends data in the form of data streams, the receiver does not know how much data the other Party sends at a time, and can ensure that the data sent at a time can be received at the same time, so the receive method works like this:
Accept a byye [] type parameter as a buffer. after a certain period of time, fill the received data in the buffer and return the actual length of received data, the actual length of the received data may be 0 (no data is received), and the length greater than 0 is smaller than the buffer length (the received data, but not as expected) is equal to the length of the buffer (indicating that the received data is greater than or equal to the expected length ).

Each receiving buffer uses the same byte [] bytemessage, and you have not checked the length of the received data. Therefore, the first time you receive data is 123456, and the second time you receive only 8, but there are still 23456 in the buffer, so the sum is 823456.

The size of the socket receiving buffer is exquisite, And the setting is too large to receive, because it will wait until as much data as possible is received before returning; if the setting is small, you need to call the Receiving Method multiple times to receive the data. The socket has an attribute that identifies the default size of the receiving buffer. You can refer to this!

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.

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.