WEBINAR: On-demand webcast
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >
Creating multithreaded TCP/IP server - one like WWW server -:) for NT becomes frequent task novadays. MFC supplies several socket classes, but, from my experience, they are just unusable for this purpose. They fail all the time, and several 1000s (!) times slower than raw Winsock interface.
Facing the challenge, I wrote my own thin wrapper classes around socket API, together with template for server, and it was surprisingly simple.
Several notes for class usage:
Server's thread dispatcher for client-server environment.
Class CWizThreadDispatcher.Uses pure virtual helper class CWizMultiThreadedWorker. Serves regular situation when requests come from many clients to the server.
Dispatcher starts to serve the client in the separate thread and continues to wait for requests. The number of parallel threads is limited for effectiveness. The actual work (whatever it is) made in the class derived from CWizMultiThreadedWorker.
Files ThreadDispatcher.CPP and ThreadDispatcher.H .
Class CWizSyncSocket.It's simple and somewhere limited encapsulation of the SOCKET handle for WinSock. It works in synchronous mode (which is suitable for multithreading program), but it can be made alertable (see CWizRawSocketListener).
Derived class CWizReadWriteSocket implements read/write via socket.
Functions ReadString and WriteString implement character string I/O and translate UNICODE to ANSI if needed.
Files RawSocket.CPP and RawSocket.H .
Class CWizRawSocketListenerderived from CWizMultiThreadedWorker to work with CWizThreadDispatcher and it uses CWizSyncSocket. For real application you should derive your class from it and implement method
BOOL ReadWrite (CWizReadWriteSocket& socket).
Files RawSocketServerWorker.CPP and RawSocketServerWorker.H .
Example of the server and the client are at RawSocketServerExample.ZIP.
Note: the class was made alertable by installing hook function via WSASetBlockingHook() - see file RawSocketServerWorker.CPP. The same solution can be recommended for any use of CWizSyncSocket.
Article moved from Internet section on January 26 1999