Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Implement the Server Waiting Loop(s) for Multiple Client Connections
    text
    copied!<p>I'm working on a Client-Server project. It should be able to handle multiple (>1000) client connections.</p> <p>The main skeleton of my server-side waiting loop looks like this:</p> <pre><code>while(server-&gt;running) { // If I remove this, my CPU use will be very intensive // but putting 10 milliseconds sleep will cause a significant delay // What's is the best practice? boost::this_thread::sleep(boost::posix_time::milliseconds(10)); if (server-&gt;connectionQueue.empty()) continue; Connection* conn = server-&gt;connectionQueue.front(); server-&gt;connectionQueue.pop(); Stream* stream = conn-&gt;HandleData(); // More steps if needed here server-&gt;connectionQueue.push(conn); } </code></pre> <p>My questions are:</p> <ol> <li><p>Currently I'm using one waiting loop for all connections. Is this correct? or what's the best practice? Should I use a waiting loop for each connection?</p></li> <li><p>If I remove the <code>thread::sleep</code>, my CPU use will be very intensive. But putting 10 milliseconds sleep will cause a significant delay, especially, if there are multiple connections. So what's is the best practice?</p></li> <li><p>I googled, and found something called <code>FD_ISSET</code>: <a href="http://linux.die.net/man/3/fd_isset" rel="nofollow">http://linux.die.net/man/3/fd_isset</a></p></li> </ol> <blockquote> <pre><code>/* According to POSIX.1-2001 */ #include &lt;sys/select.h&gt; /* According to earlier standards */ #include &lt;sys/time.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); void FD_CLR(int fd, fd_set *set); int FD_ISSET(int fd, fd_set *set); void FD_SET(int fd, fd_set *set); void FD_ZERO(fd_set *set); #define _XOPEN_SOURCE 600 #include &lt;sys/select.h&gt; int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask); </code></pre> </blockquote> <p>But I don't know how to use? How can I use it for my Socket File Descriptor?</p> <p>Not sure If I made myself clear.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload