Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 59,682 times

Contents

Downloads

Related Categories

A reusable Windows socket server class - Some example servers

Len Holgate

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.

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 consultancy services. We can provide experience in COM, Corba, C++, Windows NT and UNIX. Our speciality is the design and implementation of systems but we are happy to work with you throughout the entire project life-cycle. We are happy to quote for fixed price work, or, if required, can work for an hourly rate. We are based in London, England, but, thanks to the Internet, we can work 'virtually' anywhere...

Comments