TCP server with dispatcher

In order to handle sockets quickly, accepting new connections should be done in a separate thread. The accepted connections are stored in a queue and processed each in its own thread. As an example of such approach, a chat server is constructed. The given code is written using POCO library.

Each TCP connection is observed as a session with ID, status and corresponding socket. Session status is determined on both TCP and processor level (for instance, if user logouts, then the session must be destroyed).

Session.hpp
Session.cpp

Client request and server response are chat lines with a content. Each chat line contains a command which determines does it login, logout, receive or send a chat. With each command, other specific parameters are given in the format of URL query string.

ChatLine.hpp
ChatLine.cpp

Message is the basic element of keeping data, related to a particular session. Each request/response is transformed into a message.

Message.hpp
Message.cpp

Dispatcher keeps new connections in a queue. When one is ready to read, dispatcher handles it in a separate thread.

Dispatcher.hpp
Dispatcher.cpp

Handlers deals with a single connection until it's end.

Handler.hpp
Handler.cpp

Server accepts new connections and enqueues them so the dispatcher could process them.

Server.hpp
Server.cpp

Server is started inside main function.

main.cpp

To test the server, telnet could be used or a client similar to this one.

client.cpp

Server is tested under Linux 2.6.37 64bit/gcc 4.5.2/Poco 1.4.2p1, FreeBSD 8.0 64bit/gcc 4.2.1/Poco 1.4.2p1, Windows 7/VS 2010/Poco 1.4.2p1, compiled as specified in the Makefile.

Makefile

The presented code can be downloaded as an archive.

ready.

10 print "mail: contact at alepho.com | skype: karastojko | stackoverflow: karastojko | github: karastojko"
20 print "(c) 2009-2023 www.alepho.com"