- you should set every
pfd[i].fd = -1
, so they get ignored initially by poll(). poll(pfd, 1, 0)
is wrong and should at least bepoll(pfd, 2, 0)
or evenpoll(pfd, NSERVER, 0)
.while(i<=NSERVER)
should bewhile(i<NSERVER)
Your program probably hangs, because you loop through the pfd array, which is not initialized and containes random values for .fd and .revents, so it wants to send() or recv() on some random FD which might block. Do if(pdf[i].fd < 0) {i++; continue;}
in the i<NSERVER
loop.
You also don't set pfd[pos].events = POLLIN
on newly accepted sockets. Don't set POLLOUT
unless you have something to send, because it will trigger almost every time.