473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Mar 22 '07 #1
1 6091
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul McGuire | last post by:
Well, after 3 days of open polling, the number of additional votes have dropped off pretty dramatically. Here are the results so far: Total voters: 55 (with 3 votes each) Votes for each choice...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
6
by: Dave Spencer | last post by:
Hi all, New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the...
0
by: soaphead | last post by:
Can I use Socket.Poll for blocking socket? It is unclear in documentation if I can use this with a synchronous socket, can my code possibly turn a synchronous socket to asynchronous? It seems to...
0
by: Valerie Hough | last post by:
I have a process which accepts multiple asynchronous TCP socket connections. Periodically I want this task to check for abnormal disconnection by each client. The code that detects this correctly...
6
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i...
0
by: jinfeng_Wang | last post by:
I have writen the following souce code : m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Socket.Blocking = false; try {...
1
by: chsalvia | last post by:
I'm try to use poll() to handle network connections on Linux. As a test, I create a simple loop that polls a socket so that everytime the client sends data, the server responds. My problem is...
3
by: JDavis | last post by:
I have created a static class-level socket for use by the class member functions: static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); The Connect...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.