Winsock API Wrapper Classes

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.


Downloads

Comments

  • Can't download the source

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

    Can't download the source; The URL is incorrect

    Reply
  • Still hangs when exiting!...

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

    Originally posted by: Bruno Cahuet

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

    Reply
  • File Missing ?

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

    Originally posted by: Swordman


    the following line in main.cpp is reffering to a missing file ?
    ..\..\SocketServer\names.h

    Reply
  • Winsock 10061 Error

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

    Originally posted by: HellBringer

    That's the error I get when i try to run the client giving 127.0.0.1 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...

    Reply
  • Great Job

    Posted by Legacy on 07/31/2001 12: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).

    thanks

    Reply
  • Program hangs when closed on Win2K

    Posted by Legacy on 04/03/2001 12: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?

    Reply
  • how to eliminate the bugs!

    Posted by Legacy on 12/18/2000 12: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.....

    Reply
  • Exception Handling error

    Posted by Legacy on 08/19/2000 12: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,
    Tony

    Reply
  • Can't run server in MSVC 6

    Posted by Legacy on 08/28/1999 12: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);


    Phil

    Reply
  • Is download link broken?

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

    Originally posted by: John Gann

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

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds