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.

Download Files

Article moved from Internet section on January 26 1999



Comments

  • Compile & Linking OK

    Posted by Legacy on 03/13/2001 12:00am

    Originally posted by: Choi Young Dae

    Hi there.
    I was installed 'MS Platform SDK'.
    (VC++6 sp5 / Windows2000)

    Thanks ... Poul A Costinski.

    Reply
  • Can't run after complied with VC6.0.

    Posted by Legacy on 10/09/2000 12:00am

    Originally posted by: Lam


    Can't run after complied with VC++ 6.0.
    it shown error and terminated by OS!
    What wrong?

    Reply
  • cl.exe error

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

    Originally posted by: someOneNew


    Here is the message from debug window. Please help!Deleting intermediate files and output files for project 'SC - Win32 Release'.
    --------------------Configuration: SC - Win32 Release--------------------
    Compiling...
    StdAfx.cpp
    Compiling...
    main.cpp
    C:\Dev\cpp\RawSocketServerExample\SocketClient\main.cpp(4) : fatal error C1083: Cannot open include file: '..\..\SocketServer\names.h': No such file or directory
    RawSocket.cpp
    RawSocketServerWorker.cpp
    ThreadDispatcher.cpp
    Generating Code...
    Error executing cl.exe.

    SC.exe - 1 error(s), 0 warning(s)


    Reply
  • Read-Only file?

    Posted by Legacy on 02/11/2000 12:00am

    Originally posted by: chanm7

    I am trying to comment out the two typedef lines, but it says it's a read-only project?
    Correct me if i'm wrong

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

Top White Papers and Webcasts

  • 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 …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds