wyrmmage
2007.01.23, 04:56 PM
So, I'm building a server that accepts Windows and (hopefully soon) Mac connections; although I'm programming the server in WinSock, I believe that the design would be pretty much the same on all platforms. I asked a friends who has more experience in this field than I do, and he said that a good way to design my server would be to either:
First Design:
Have a thread that was entirely in a while(1==1) loop, that listen()'ed for incoming requests, and then put them on the stack, and then a seperate thread that accept()'ed any pending connections and then went through all of the connections that had already been accepted and did any appropriate actions (like recv(), send(), updating the database, etc.).
Second design:
Have a thread that was in a while(1==1) loop that listen()'ed for incoming requests, then immediately acccept()'ed them and spawned a new thread for each connection. Each connection thread would then be in a loop until the client disconnected from the server, then the thread would be destroyed; inside each connection thread, send(), recv(), and so forth actions would be performed.
So, which of these is better? Are there any huge flaws that I am missing, or are these designs both completely off the track? I did skim the articles in the Sticky at the top of the board, although I could re-read them in detail if necessary.
Thanks :)
-wyrmmage
First Design:
Have a thread that was entirely in a while(1==1) loop, that listen()'ed for incoming requests, and then put them on the stack, and then a seperate thread that accept()'ed any pending connections and then went through all of the connections that had already been accepted and did any appropriate actions (like recv(), send(), updating the database, etc.).
Second design:
Have a thread that was in a while(1==1) loop that listen()'ed for incoming requests, then immediately acccept()'ed them and spawned a new thread for each connection. Each connection thread would then be in a loop until the client disconnected from the server, then the thread would be destroyed; inside each connection thread, send(), recv(), and so forth actions would be performed.
So, which of these is better? Are there any huge flaws that I am missing, or are these designs both completely off the track? I did skim the articles in the Sticky at the top of the board, although I could re-read them in detail if necessary.
Thanks :)
-wyrmmage