Poll Loop (Asynchronous IO)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chsalvia@gmail.com

    Poll Loop (Asynchronous IO)

    I'm trying to implement a simple poll loop which asynchronously
    fetches a list of URLs. Since I'm used to working with multiple
    threads, I have little experience using single-threaded IO
    multiplexing. I wrote a small program to try to get it to work, but
    I'm having difficulty.

    I connect to a bunch of non-blocking sockets, then poll them for
    activity. On POLLOUT, I send an HTTP request. On POLLIN, I recv
    incoming data. But the loop doesn't work. The POLLOUT even occurs
    over and over again, on each socket, and POLLIN never occurs.

    pollfd* pfd = (pollfd*) malloc(nsock * sizeof(pollfd)) ;
    for (int i = 0; i < nsock; ++i) {
    pfd[i].fd = sock[i];
    pfd[i].events = POLLOUT | POLLIN;
    pfd[i].revents = 0;
    }

    for (int i = 0; i < nsock; ++i) {
    connect(sock[i], (sockaddr*) &m_addr[i], sizeof(m_addr)) ;
    }

    dbuffer buffer;

    int timeout = -1;
    cout << "Polling... " << endl;
    for(;;) {
    if (poll(pfd, nsock, timeout) 0) {
    for (int i = 0; i < nsock; ++i) {
    if (pfd[i].revents & POLLOUT) {
    send(sock[i], request, request_length, 0);
    }
    if (pfd[i].revents & POLLIN) {
    cout << "Found POLL IN EVENT" << endl;
    int status;
    do {
    if (status = buffer.recv(soc k[i], buf, maxrecv, 0) {
    if (errno != EWOULDBLOCK) break;
    }
    } while (status != 0);
    }
    }
    }
    else {
    if (errno == EINTR) continue;
    perror("poll()" );
    }
    }

  • Ian Collins

    #2
    Re: Poll Loop (Asynchronous IO)

    chsalvia@gmail. com wrote:
    I'm trying to implement a simple poll loop which asynchronously
    fetches a list of URLs. Since I'm used to working with multiple
    threads, I have little experience using single-threaded IO
    multiplexing. I wrote a small program to try to get it to work, but
    I'm having difficulty.
    >
    Try comp.unix.progr ammer.

    --
    Ian Collins.

    Comment

    Working...