The number of unread bytes of a Linux socket or file descriptor Fionread,msg_peek flag

Source: Internet
Author: User
Tags readable

Fionread is how many bytes are returned in the buffer .
Input has an input buffer, using the
int nread;
IOCTL (0,fionread,&nread);

can get the number of bytes in the buffer to be read. The value is placed in the Nread .

Then you can read it. Nread = Read (0,buffer,nread);

============================================================================

The MSG_PEEK flag can be used to read the data that is readable in the socket receive queue, which is used in some cases, such as checking the length of the data in the socket receive queue first to avoid blocking , and then taking the appropriate action.
Of course, other methods, such as non-blocking I/O, can also be taken without blocking.

For TCP sockets
The MSG_PEEK flag copies the readable data from the socket receive queue to the buffer, but does not reduce the data in the socket receive queue , which is common: for example, after calling recv or read, which causes the data in the socket receive queue to be read down and The MSG_PEEK flag is specified to obtain a readable data length from the return value and does not reduce the data in the socket receive buffer, so it can be read by other parts of the program .
Note: Suppose that you specify the MSG_PEEK flag to call recv for a TCP socket with a buffer length of 1024 bytes, return 100, and if you call Recv again, the return value may exceed
The length increases because there may be new data arrivals between the two invocations.  

For UDP sockets

If Msg_peek is specified, the first recv plus the flag is read once, and then the flag is read again, even though there are several reports that the receive queue for the socket is joined in the middle of the two calls, and the two return values are identical.

The number of unread bytes of the

Linux socket or file descriptor Fionread,msg_peek flag

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.