mercredi 30 décembre 2015

Behavior of the poll() system call and receiving or sending data afterwards

Lets consider following piece of code

pollfd file_descriptors[1];
file_descriptors[0].fd = sock_fd;
file_descriptors[0].events = POLLIN | POLLPRI;

int return_value = poll(file_descriptors, 1, 0);

if (return_value == -1) { cerr << strerror(errno); }
else if (return_value == 0) { cerr << "No data available to be read"; }
else { 
    int received = 0;
    if (file_descriptors[0].revents & POLLIN) {
        received += recv(sock_fd, buff, 1024, 0); 
    }

    if (file_descriptors[0].revents & POLLPRI) {
        recv(sock_fd, buff + received, 1024 - received, MSG_OOB);
    }
}

Now I have three questions regarding the above code.

  1. If the call to poll() returns neither -1 nor 0 and sets the POLLIN flag in the bitmap revents for the first entry in the file_descriptors array, then will the call to recv() block? If no, then will the data be read in instantaneously?
  2. Assuming the call to poll() goes the same way as mentioned above. How much data is going to be read in? Is it just going to be the same as a regular call to recv()? i.e. an arbitrary (to the programmer) amount less than or equal to 1024 in the above case. Then if I want to poll() before reading again, do I just repeat from the first invocation of poll()?
  3. What exactly is out-of-band data? I apologize for asking this here but I did look this up a little on Google and I didn't understand when exactly I can expect there to be out of band data in the socket to be read. Is the above way of reading in out-of-band data along with regular data correct?

Thank you!

Aucun commentaire:

Enregistrer un commentaire