Virtual Developer Workshop: Containerized Development with Docker

This sample was contributed by Poul A Costinski

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. The complete example project is in file RawSocketServerExample.ZIP.

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 CWizRawSocketListener

derived 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.



  • Can't download the source

    Posted by elmajdouli on 05/26/2004 11:16pm

    Can't download the source; The URL is incorrect

  • Still hangs when exiting!...

    Posted by Legacy on 02/23/2003 08:00am

    Originally posted by: Bruno Cahuet

    The problem located during discarder.waitforshutdown
    Don't know how to fix it.

  • File Missing ?

    Posted by Legacy on 11/11/2002 08:00am

    Originally posted by: Swordman

    the following line in main.cpp is reffering to a missing file ?

  • Winsock 10061 Error

    Posted by Legacy on 02/16/2002 08:00am

    Originally posted by: HellBringer

    That's the error I get when i try to run the client giving 1054-9999 (any port in this range). I read what meaning is in MSDN, but it dowsn't help me much. The Winsock function which throws this error is ::connect.
    Any ideas? Help will be appriciated...

  • Great Job

    Posted by Legacy on 07/31/2001 07:00am

    Originally posted by: Mathe Lorant

    Yes this is a great Job. Saves a lot of time !
    In Java is very easy (due to sintax) to write such functions but in windows MS c++ world it was simply too ugly and complicated to do it (CSocket isn't good for real server applications).


  • Program hangs when closed on Win2K

    Posted by Legacy on 04/03/2001 07:00am

    Originally posted by: John Martin

    The SocketServer program hangs when you hit the Stop button and the process must be killed using the Windows Task Manager. Does anyone know why and more importantly how to work around this?

  • how to eliminate the bugs!

    Posted by Legacy on 12/18/2000 08:00am

    Originally posted by: rickyu

    There will be two major errors when try to compile the Project. You can eliminate these errors as follows:
    1 remarks these two lines in the file afxstd.h, because VC++6 has already define those.
    //typedef signed long INT32 ;
    //typedef unsigned long UINT32;

    2 copy UAFXCW.lib(Unicode release version needed), UAFXCWD.lib(Unicode debug version needed) and UAFXCWD.pdb(debug information) from /VC98/MFC/Lib to the project directory.
    3 We go. Ho.Ho.Ho.....

  • Exception Handling error

    Posted by Legacy on 08/19/2000 07:00am

    Originally posted by: Tony Boswell

    I dont know if you still check yer comments on this page, but..............
    I compiled the client, it seems to be functioning okay although I havent made a connection yet to test it.
    The serversocket is my problem right now, its compiling great, no errors, but warnings:

    LINK : warning LNK4089: all references to "SHELL32.dll" discarded by /OPT:REF

    LINK : warning LNK4089: all references to "comdlg32.dll" discarded by /OPT:REF

    I Dont know if this is the problem, but when I goto run it run it it crashes with an error. This could be due to the fact that I have no idea where in the code I am to insert my
    Ip address and port #.

    The error is as follows:
    First-chance exception in SocketServer.exe: 0xC0000005: Access Violation.

    Can ya help me out here? Thanks,

  • Can't run server in MSVC 6

    Posted by Legacy on 08/28/1999 07:00am

    Originally posted by: phord

    I can get it to compile now, but I get several ASSERTS in MFC when I try to run the server under MSVC 6.  Anyone have any ideas?

    Here are the first two asserts that trip in auxdata.cpp:

    // Standard cursors
    hcurWait = ::LoadCursor(NULL, IDC_WAIT);
    hcurArrow = ::LoadCursor(NULL, IDC_ARROW);
    ASSERT(hcurWait != NULL);
    ASSERT(hcurArrow != NULL);


  • Is download link broken?

    Posted by Legacy on 03/31/1999 08:00am

    Originally posted by: John Gann

    Has anyone tried to download this item lately? It seems to be broken.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date