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