lib/socket-util-unix.c
/* Drain all the data currently in the receive queue of a datagram socket (and
 * possibly additional data).  There is no way to know how many packets are in
 * the receive queue, but we do know that the total number of bytes queued does
 * not exceed the receive buffer size, so we pull packets until none are left
 * or we've read that many bytes. */
Drain all the data currently in the receive queue of a datagram socket (and
possibly additional data).  There is no way to know how many packets are in
the receive queue, but we do know that the total number of bytes queued does
not exceed the receive buffer size, so we pull packets until none are left
or we've read that many bytes.
Drain all the data currently in the receive queue of a datagram socket ( and possibly additional data ) . There is no way to know how many packets are in the receive queue but we do know that the total number of bytes queued does not exceed the receive buffer size so we pull packets until none are left or we ' ve read that many bytes .
int
drain_rcvbuf(int fd)
{
    int rcvbuf;
    rcvbuf = get_socket_rcvbuf(fd);
    if (rcvbuf < 0) {
        return -rcvbuf;
    }
    while (rcvbuf > 0) {
        char buffer[LINUX ? 1 : 2048];
        ssize_t n_bytes = recv(fd, buffer, sizeof buffer,
                               MSG_TRUNC | MSG_DONTWAIT);
        if (n_bytes <= 0 || n_bytes >= rcvbuf) {
            break;
        }
        rcvbuf -= n_bytes;
    }
    return 0;
}
int drain_rcvbuf ( int fd ) { int rcvbuf ; rcvbuf = get_socket_rcvbuf ( fd ) ; if ( rcvbuf < 0 ) { return - rcvbuf ; } while ( rcvbuf > 0 ) { char buffer [ LINUX ? 1 : 2048 ] ; ssize_t n_bytes = recv ( fd , buffer , sizeof buffer , MSG_TRUNC | MSG_DONTWAIT ) ; if ( n_bytes <= 0 || n_bytes >= rcvbuf ) { break ; } rcvbuf -= n_bytes ; } return 0 ; }
Drain all the data currently in the receive queue of a datagram socket (and
 possibly additional data).
Drain all the data currently in the receive queue of a datagram socket ( and possibly additional data ) .
/* In Linux, specifying MSG_TRUNC in the flags argument causes the
         * datagram length to be returned, even if that is longer than the
         * buffer provided.  Thus, we can use a 1-byte buffer to discard the
         * incoming datagram and still be able to account how many bytes were
         * removed from the receive buffer.
         *
         * On other Unix-like OSes, MSG_TRUNC has no effect in the flags
         * argument. */