Library tutorials & articles

A reusable Windows socket server class

Some example servers

We now have a framework for creating servers. The user needs to provide a worker thread class that is derived from CSocketServer::WorkerThread and a socket server that's derived from CSocketServer. These classes could look something like this:

class CSocketServerWorkerThread : public CSocketServer::WorkerThread
{
   public :

      CSocketServerWorkerThread(
         CIOCompletionPort &iocp);

   private :

      virtual void ReadCompleted(
         CSocketServer::Socket *pSocket,
         CIOBuffer *pBuffer);
};

class CMySocketServer : CSocketServer
{
   public :

      CMySocketServer (
         unsigned long addressToListenOn,
         unsigned short portToListenOn);

   private :

      virtual WorkerThread *CreateWorkerThread(
         CIOCompletionPort &iocp);

      virtual SOCKET CreateListeningSocket(
         unsigned long address,
         unsigned short port);

      virtual void OnConnectionEstablished(
         Socket *pSocket,
         CIOBuffer *pAddress);
};

Implementations for CreateListeningSocket() and OnConnectionEstablished() have already been presented. CreateWorkerThread() is as simple as this:

   CSocketServer::WorkerThread *CMySocketServer::CreateWorkerThread(
      CIOCompletionPort &iocp)
   {
      return new CSocketServerWorkerThread(iocp);
   }

Which leaves us with the implementation of our worker thread's ReadCompleted() method. This is where the server handles incoming data and, in the case of a simple Echo server ;) it could be as simple as this:

   void CSocketServerWorkerThread::ReadCompleted(
      CSocketServer::Socket *pSocket,
      CIOBuffer *pBuffer)
   {
      pSocket->Write(pBuffer);
   }
YAES - Yet another echo server

A complete echo server is available for download in SocketServer1.zip. The server simply echos the incoming byte stream back to the client. In addition to implementing the methods discussed above the socket server and worker thread derived classes also implement several 'notifciation' methods that the server and worker thread classes call to inform the derived class of various internal goings on. The echo server simply outputs a message to the screen (and log file) when these notifications occur but the idea behind them is that the derived class can use them to report on internal server state via performance counters or suchlike. You can test the echo server by using telnet. Simply telnet to localhost on port 5001 (the port that the sample uses by default) and type stuff and watch it get typed back at you. The server runs until a named event is set and then shuts down. The very simple Server Shutdown program, available in ServerShutdown.zip, provides an off switch for the server.

Comments

  1. 08 Jul 2002 at 06:55
    Is there any API of C which can help me in session tracking?
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of A reusable Windows socket server class.

Leave a comment

Sign in or Join us (it's free).

Len Holgate Len has been programming for over 20 years, having first started with a Sinclair ZX-80. Now he runs his own consulting company, JetByte Limited. JetByte provides contract programming and consultanc...

Related discussion

Related podcasts

  • Listener Feedback 67

    This mailbag episode includes FASM, scripts, sockets, SSL/TLS, HTTPS, Windows 7's XP mode, and more. Security Now wiki shownotes For 16kpbs versions, transcripts, and notes (including fixes), visit Steve's site: grc.com, also the home of the best disk maintenance and recovery utility ever written...

We'd love to hear what you think! Submit ideas or give us feedback