<p>I am looking for any strategies people use when implementing server applications that service client TCP (or UDP) requests: design patterns, implementation techniques, best practices, etc.</p>

<p>Let's assume for the purposes of this question that the requests are relatively long-lived (several minutes) and that the traffic is time sensitive, so no delays are acceptable in responding to messages.  Also, we are both servicing requests from clients and making our own connections to other servers.</p>

<p>My platform is .NET, but since the underlying technology is the same regardless of platform, I'm interested to see answers for any language.</p>

[{'answer_id': 32242, 'author': 'Mark Renouf', 'author_id': 758, 'author_profile': 'https://Stackoverflow.com/users/758', 'pm_score': 4, 'selected': True, 'text': '<p>The modern approach is to make use of the operating system to multiplex many network sockets for you, freeing your application to only processing active connections with traffic.</p>\n\n<p>Whenever you open a socket it\'s associated it with a selector. You use a single thread to poll that selector. Whenever data arrives, the selector will indicate the socket which is active, you hand off that operation to a child thread and continue polling.</p>\n\n<p>This way you only need a thread for each concurrent operation. Sockets which are open but idle will not tie up a thread.</p>\n\n<ul>\n<li><a href="http://www.techrepublic.com/article/using-the-select-and-poll-methods/" rel="nofollow noreferrer">Using the select() and poll() methods</a></li>\n<li><a href="http://www.onjava.com/pub/a/onjava/2004/09/01/nio.html" rel="nofollow noreferrer">Building Highly Scalable Servers with Java NIO</a></li>\n</ul>\n'}, {'answer_id': 32255, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 0, 'selected': False, 'text': '<p>G\'day,</p>\n\n<p>I\'d start by looking at the metaphor you want to use for your thread framework.</p>\n\n<p>Maybe "leader follower" where a thread is listening for incoming requests and when a new request comes in it does the work and the next thread in the pool starts listening for incoming requests.</p>\n\n<p>Or thread pool where the same thread is always listening for incoming requests and then passing the requests over to the next available thread in the thread pool.</p>\n\n<p>You might like to visit the <a href="http://www.cs.wustl.edu/~schmidt/ACE-papers.html#reactor" rel="nofollow noreferrer">Reactor</a> section of the Ace Components to get some ideas.</p>\n\n<p>HTH.</p>\n\n<p>cheers,\nRob</p>\n'}, {'answer_id': 32400, 'author': 'Hugo', 'author_id': 972, 'author_profile': 'https://Stackoverflow.com/users/972', 'pm_score': 2, 'selected': False, 'text': '<p>A more sophosticated aproach would be to use IO Completion ports. (Windows)\nWith IO Completion ports you leave to the operating system to manage polling, which lets it potentially use very high level of optimization with NIC driver support.\nBasically, you have a queue of network operations which is OS managed, and provide a callback function which is called when the operation completes. A bit like (Hard-drive) DMA but for network.</p>\n\n<p>Len Holgate wrote an eccelent series on IO completion ports a few years ago on Codeproject:\n<a href="http://www.codeproject.com/KB/IP/jbsocketserver2.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/IP/jbsocketserver2.aspx</a></p>\n\n<p>And \nI found an article on IO completion ports for .net (haven\'t read it though)\n<a href="http://www.codeproject.com/KB/cs/managediocp.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cs/managediocp.aspx</a></p>\n\n<p>I would also say that it is easy to use completion ports compared to try and write a scaleable alternative. The problem is that they are only available on NT (2000, XP, Vista)</p>\n'}, {'answer_id': 81068, 'author': 'Len Holgate', 'author_id': 7925, 'author_profile': 'https://Stackoverflow.com/users/7925', 'pm_score': 2, 'selected': False, 'text': '<p>If you were using C++ and the Win32 directly then I\'d suggest that you read up about overlapped I/O and I/O Completion ports. I have a free C++, IOCP, client/server framework with complete source code, see <a href="http://www.serverframework.com/products---the-free-framework.html" rel="nofollow noreferrer">here</a> for more details.</p>\n\n<p>Since you\'re using .Net you should be looking at using the asynchronous socket methods so that you don\'t need have a thread for every connection; there are several links from this blog posting of mine that may be useful starting points:  <a href="http://www.lenholgate.com/blog/2005/07/disappointing-net-sockets-article-in-msdn-magazine-this-month.html" rel="nofollow noreferrer">http://www.lenholgate.com/blog/2005/07/disappointing-net-sockets-article-in-msdn-magazine-this-month.html</a> (some of the best links are in the comments to the original posting!)</p>\n'}]
['https://Stackoverflow.com/questions/32198', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1219/']