Example Servers - Echo Server Multi Buffer Write

This example shows you how to build another simple server. The basic structure is very similar to the Basic Echo Server example and you should go and read about that first and have a good understanding of how everything fits together. This document will only cover the differences between the Basic Echo Server example and this example.

This example is shipped with all licensed versions of the server framework and it requires the core server framework libraries (see here for licensing options). You can always download the latest version of this example from here; and although you will need the correct libraries to be able to build it you can look at the example code and see how it works and perhaps get ideas from it. A compiled, unicode release, build of this example is available on request if you require it for performance analysis of the framework.

The main difference between this server and the Basic Echo Server is that this server sends data using a multi-buffer. This allows it to efficiently send data which includes common header and trailer elements, though multi-buffers can be used in any situation where you need a single write call to write the data from multiple buffer objects.

You will notice that in ServerMain we provide the CSocketServer object with two buffer allocators. One is the normal allocator and one is a CMultiBufferHandleAllocator. The later is used to allocate a multi-buffer into which you can plug the buffers that you wish to send.

During start up the server creates two static buffers of data that it will send with every response, the header buffer and the trailer buffer. It populates these with data. Then when the time comes to send a response to the client it allocates a multi-buffer, plugs in the header, trailer and data buffers and writes the data to the client.

 void CSocketServer::EchoMessage(
    IStreamSocket &socket,
    IBuffer &buffer) 
 {
    DEBUG_ONLY(Output(_T("Data Echoed -\r\n") + DumpData(buffer.GetMemory(), buffer.GetUsed(), 60, true)));
 
    CMultiBufferHandle *pHandle = m_bufferHandleAllocator.AllocateHandle();
 
    pHandle->AllocateSpace(3);
 
    pHandle->SetAt(0, m_headerBuffer.GetRef());
    pHandle->SetAt(1, buffer);
    pHandle->SetAt(2, m_trailerBuffer.GetRef());
 
    socket.Write(*pHandle);
 
    pHandle->Release();
 }

You can test this server by telnetting to it and watching how the characters that you enter are echoed back to you with the common header and trailer attached. Note that there is no data copying required for the common header and trailer buffers.
Generated on Thu Mar 26 16:38:10 2009 for JetByte Socket Tools - v6.0 by  doxygen 1.5.3