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.

You might also like...

Comments

About the author

Len Holgate United Kingdom

Len has been programming for over 20 years, having first started with a Sinclair ZX-80. Now he runs his own consulting company, JetByte Li...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger