Poll Loop (Asynchronous IO)

Discussion in 'C Programming' started by chsalvia@gmail.com, Mar 22, 2007.

  1. Guest

    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.fd = sock;
    pfd.events = POLLOUT | POLLIN;
    pfd.revents = 0;
    }

    for (int i = 0; i < nsock; ++i) {
    connect(sock, (sockaddr*) &m_addr, 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.revents & POLLOUT) {
    send(sock, request, request_length, 0);
    }
    if (pfd.revents & POLLIN) {
    cout << "Found POLL IN EVENT" << endl;
    int status;
    do {
    if (status = buffer.recv(sock, buf, maxrecv, 0) {
    if (errno != EWOULDBLOCK) break;
    }
    } while (status != 0);
    }
    }
    }
    else {
    if (errno == EINTR) continue;
    perror("poll()");
    }
    }
    , Mar 22, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    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.programmer.

    --
    Ian Collins.
    Ian Collins, Mar 22, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. antonio bergnoli

    ghdl poll

    antonio bergnoli, Dec 11, 2005, in forum: VHDL
    Replies:
    12
    Views:
    1,224
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,209
    Patricia Shanahan
    Dec 23, 2006
  3. birdsong

    select.poll.poll() never blocks

    birdsong, Feb 12, 2009, in forum: Python
    Replies:
    2
    Views:
    438
    birdsong
    Feb 12, 2009
  4. Jean-Paul Calderone

    Re: select.poll.poll() never blocks

    Jean-Paul Calderone, Feb 12, 2009, in forum: Python
    Replies:
    3
    Views:
    425
    birdsong
    Feb 12, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    342
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page