This tip shows a very simple non-blocking TCP echo server using Windows Input/Output Completion Ports, capable of handling any number of clients simultaneously.
IOCPs are generally considered the best (some would say the only) way of writing high-performance, highly-scalable network servers in Windows. But there are no simple examples of their use readily available in the internet.
This tip intends to fulfill this gap, by providing a very simple C-language IOCP server example. There are no classes to learn, no other Windows API to worry about, no thread stuff to complicate matters, just the two functions relating to IOCPs:
. Also, of course, the usual sockets API are used, in the Winsock asynchronous flavor:
The general flow of control of the echo server is described in the steps below:
1. initialize Winsock (WSAStartup)
2. create the I/O Completion Port (CreateIoCompletionPort)
3. create the listening socket (listen)
4. associate the listening socket with the IOCP (CreateIoCompletionPort)
5. bind the listening socket (bind)
6. start listening (listen)
7. create a pointer to the AcceptEx function (!)
8. start accepting a new connection (asynchronous AcceptEx)
9. wait for a completion port status (GetQueuedCompletionStatus)
9.1 if the completion status is for an accept (an AcceptEx has completed)
9.1.1 "update the context" of the new socket (whatever that is...)
9.1.2 associate the new socket with the completion port
9.1.3 start reading from the new socket (asynchronous WSARecv)
9.1.4 start accepting another connection (asynchronous AcceptEx)
9.1.5 go to step 9
9.2 if the completion status is for a read (a WSARecv has completed)
9.2.1 start writing to the socket through which the data arrived,
echoing back the data (asynchronous WSASend)
9.2.2 go to step 9
9.3 if the completion status is for a write (a WSASend has completed)
9.3.1 start reading again from the socket that was used to send the data
9.3.2 go to step 9
That's it! Well, there are all those pesky error situations that have to be handled, and there's also some code to track the connections, but all in all, the above pseudo-code sums up most of the program code. One thing to note though is that all operations must be "overlapped" (a kind of asynchronous I/O operation in Windows).
Also, there's a small client program (not using IOCPs) that can be used to test the echo server.
IOCPs are easy to use, and the program shipped with this tip intends to show how to do it.
You can download the code from here