By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,923 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,923 IT Pros & Developers. It's quick & easy.

Poll Loop (Asynchronous IO)

P: n/a
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(sock[i], buf, maxrecv, 0) {
if (errno != EWOULDBLOCK) break;
}
} while (status != 0);
}
}
}
else {
if (errno == EINTR) continue;
perror("poll()");
}
}

Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ch******@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.programmer.

--
Ian Collins.
Mar 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.