<p>What is the normal way people writing network code in Delphi use Windows-style overlapped asynchronous socket I/O?</p>

<p>Here's my prior research into this question:</p>

<p>The <a href="http://indyproject.org/" rel="noreferrer">Indy</a> components seem entirely synchronous. On the other hand, while ScktComp unit does use WSAAsyncSelect, it basically only asynchronizes a BSD-style multiplexed socket app. You get dumped in a single event callback, as if you had just returned from select() in a loop, and have to do all the state machine navigation yourself.</p>

<p>The .NET situation is considerably nicer, with Socket.BeginRead / Socket.EndRead, where the continuation is passed directly to Socket.BeginRead, and that's where you pick back up. A continuation coded as a closure obviously has all the context you need, and more.</p>

[{'answer_id': 37282, 'author': 'Mike', 'author_id': 1743, 'author_profile': 'https://Stackoverflow.com/users/1743', 'pm_score': 2, 'selected': False, 'text': '<p>I have found that Indy, while a simpler concept in the beginning, is awkward to manage due to the need to kill sockets to free threads at application termination.   In addition, I had the Indy library stop working after an OS patch upgrade.   ScktComp works well for my application.</p>\n'}, {'answer_id': 38099, 'author': 'Roddy', 'author_id': 1737, 'author_profile': 'https://Stackoverflow.com/users/1737', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>What is the normal way people writing\n  network code in Delphi use\n  Windows-style overlapped asynchronous\n  socket I/O?</p>\n</blockquote>\n\n<p>Well, Indy has been the \'standard\' library for socket I/O for a long while now - and it\'s based on blocking sockets. This means if you want asynchronous behaviour, you use additional thread(s) to connect/read/write data. To my mind this is actually a major advantage, as there\'s no need to manage any kind of state machine navigation, or worry about callback procs or similar stuff. I find the logic of my \'reading\' thread is less cluttered and much more portable than non-blocking sockets would allow.</p>\n\n<p>Indy <strong>9</strong> has been mostly bombproof, fast and reliable for us. However the move to Indy 10 for Tiburon is causing me a little concern.</p>\n\n<blockquote>\n  <p>@Mike: "...the need to kill sockets to free threads...".</p>\n</blockquote>\n\n<p>This made go "huh?" until I remembered our threading library uses an exception-based technique to kill \'waiting\' threads safely. We call <a href="http://msdn.microsoft.com/en-us/library/ms684954(VS.85).aspx" rel="nofollow noreferrer">QueueUserAPC</a> to queue a function which raises a C++ exception (NOT derived from class Exception) which should only be caught by our thread wrapper procedure. All destructors get called so the threads all terminate cleanly and tidy up on the way out.</p>\n'}, {'answer_id': 38339, 'author': 'Barry Kelly', 'author_id': 3712, 'author_profile': 'https://Stackoverflow.com/users/3712', 'pm_score': 2, 'selected': False, 'text': "<p>@Roddy - Synchronous sockets are not what I'm after. Burning a whole thread for the sake of a possibly long-lived connection means you limit the amount of concurrent connections to the number of threads that your process can contain. Since threads use a lot of resources - reserved stack address space, committed stack memory, and kernel transitions for context switches - they do not scale when you need to support hundreds of connections, much less thousands or more.</p>\n"}, {'answer_id': 39138, 'author': 'Roddy', 'author_id': 1737, 'author_profile': 'https://Stackoverflow.com/users/1737', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>"Synchronous sockets are not what I\'m after."</p>\n</blockquote>\n\n<p>Understood - but I think in that case the answer to your original question is that there just isn\'t a Delphi <em>idiom</em> for async socket IO because it\'s actually a highly specialized and uncommon requirement.</p>\n\n<p>As a side issue, you might find these links interesting. They\'re both a little old, and more *nxy than Windows. The second one implies that - in the right environment - threads might not be as bad as you think.</p>\n\n<p><a href="http://www.kegel.com/c10k.html" rel="nofollow noreferrer">The C10K problem</a></p>\n\n<p><a href="http://www.usenix.org/events/hotos03/tech/vonbehren.html" rel="nofollow noreferrer">Why Events Are A Bad Idea (for High-concurrency Servers)</a></p>\n'}, {'answer_id': 42408, 'author': 'Chris Miller', 'author_id': 206, 'author_profile': 'https://Stackoverflow.com/users/206', 'pm_score': 0, 'selected': False, 'text': '<p>Indy uses synchronous sockets because it\'s a simpler way of programming.  The asynchronous socket  blocking was something added to the winsock stack back in the Windows 3.x days.  Windows 3.x did not support threads and there you couldn\'t do socket I/O without threads.  For some additional information about why Indy uses the blocking model, please see <a href="http://www.swissdelphicenter.ch/en/showarticle.php?id=4" rel="nofollow noreferrer">this article</a>.</p>\n\n<p>The .NET Socket.BeginRead/EndRead calls are using threads, it\'s just managed by the Framework instead of by you.</p>\n\n<p>@Roddy, Indy 10 has been bundled with Delphi since at Delphi 2006.  I found that migrating from Indy 9 to Indy 10 to be a straight forward task.</p>\n'}, {'answer_id': 44536, 'author': 'Barry Kelly', 'author_id': 3712, 'author_profile': 'https://Stackoverflow.com/users/3712', 'pm_score': 1, 'selected': False, 'text': '<p>@Chris Miller - What you\'ve stated in your answer is factually inaccurate.</p>\n\n<p>Windows message-style async, as available through WSAAsyncSelect, is indeed largely a workaround for lack of a proper threading model in Win 3.x days.</p>\n\n<p>.NET Begin/End, however, is <strong>not</strong> using extra threads. Instead, it is using overlapped I/O, using the extra argument on WSASend / WSARecv, specifically the overlapped completion routine, to specify the continuation.</p>\n\n<p>This means that the .NET style harnesses the Windows OS\'s async I/O support to <strong>avoid</strong> burning a thread by blocking on a socket.</p>\n\n<p>Since threads are generally speaking expensive (unless you specify a very small stack size to CreateThread), having threads blocking on sockets will stop you from scaling to 10,000s of concurrent connections.</p>\n\n<p>This is why it\'s important that async I/O be used if you want to scale, and also why .NET is <strong>not</strong>, I repeat, is <strong>not</strong>, simply "using threads, [...] just managed by the Framework".</p>\n'}, {'answer_id': 44613, 'author': 'Barry Kelly', 'author_id': 3712, 'author_profile': 'https://Stackoverflow.com/users/3712', 'pm_score': 1, 'selected': False, 'text': '<p>@Roddy - I\'ve already read the links you point to, they are both referenced from Paul Tyma\'s presentation "Thousands of Threads and Blocking I/O - The old way to write Java Servers is New again".</p>\n\n<p>Some of the things that don\'t necessarily jump out from Paul\'s presentation, however, are that he specified -Xss:48k to the JVM on startup, and that he\'s assuming that the JVM\'s NIO implementation is efficient in order for it to be a valid comparison.</p>\n\n<p>Indy does <strong>not</strong> specify a similarly shrunken and tightly constrained stack size. There are no calls to BeginThread (the Delphi RTL thread creation routine, which you should use for such situations) or CreateThread (the raw WinAPI call) in the Indy codebase.</p>\n\n<p>The default stack size is stored in the PE, and for the Delphi compiler it defaults to 1MB of reserved address space (space is committed page by page by the OS in 4K chunks; in fact, the compiler needs to generate code to touch pages if there are >4K of locals in a function, because the extension is controlled by page faults, but only for the lowest (guard) page in the stack). That means you\'re going to run out of address space after max 2,000 concurrent threads handling connections.</p>\n\n<p>Now, you can change the default stack size in the PE using the {$M minStackSize [,maxStackSize]} directive, but that will affect <strong>all</strong> threads, including the main thread. I hope you don\'t do much recursion, because 48K or (similar) isn\'t a lot of space.</p>\n\n<p>Now, whether Paul is right about non-performance of async I/O for Windows in particular, I\'m not 100% sure - I\'d have to measure it to be certain. What I do know, however, is that arguments about threaded programming being easier than async event-based programming, are presenting a <strong>false dichotomy</strong>.</p>\n\n<p>Async code doesn\'t <strong>need</strong> to be event-based; it can be continuation-based, like it is in .NET, and if you specify a closure as your continuation, you get state maintained for you for free. Moreover, conversion from linear thread-style code to continuation-passing-style async code can be made mechanical by a compiler (CPS transform is mechanical), so there need be no cost in code clarity either.</p>\n'}, {'answer_id': 53369, 'author': 'ddowns', 'author_id': 5201, 'author_profile': 'https://Stackoverflow.com/users/5201', 'pm_score': 1, 'selected': True, 'text': '<p>For async stuff try ICS</p>\n\n<p><a href="http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html" rel="nofollow noreferrer">http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html</a></p>\n'}, {'answer_id': 57588, 'author': 'Jody Dawkins', 'author_id': 1234, 'author_profile': 'https://Stackoverflow.com/users/1234', 'pm_score': 0, 'selected': False, 'text': "<p>With the ScktComp classes, you need to use a ThreadBlocking server rather than an a NonBlocking server type. Use the OnGetThread event to hand off the ClientSocket param to a new thread of your devising. Once you've instantiated an inherited instance of TServerClientThread you'll create a instance of TWinSocketStream (inside the thread) which you can use to read and write to the socket. This method gets you away from trying to process data in the event handler. These threads could exist for just the short period need to read or write, or hang on for the duration for the purpose of being reused.</p>\n\n<p>The subject of writing a socket server is fairly vast. There are many techniques and practices you could choose to implement. The method of reading and writing to the same socket with in the TServerClientThread is straight forward and fine for simple applications. If you need a model for high availability and high concurrency then you need to look into patterns like the Proactor pattern.</p>\n\n<p>Good luck!</p>\n"}, {'answer_id': 437602, 'author': 'Serguzest', 'author_id': 37601, 'author_profile': 'https://Stackoverflow.com/users/37601', 'pm_score': 1, 'selected': False, 'text': '<p>There is a free IOCP (completion ports) socket components : <a href="http://www.torry.net/authorsmore.php?id=7131" rel="nofollow noreferrer">http://www.torry.net/authorsmore.php?id=7131</a> (source code included)</p>\n\n<blockquote>\n  <p>"By Naberegnyh Sergey N.. High\n  performance socket server based on\n  Windows Completion Port and with using\n  Windows Socket Extensions. IPv6\n  supported. "</p>\n</blockquote>\n\n<p>i\'ve found it while looking better components/library to rearchitecture my little instant messaging server. I haven\'t tried it yet but it looks good coded as a first impression.</p>\n'}]
['https://Stackoverflow.com/questions/37185', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3712/']