doing TODAY and not getting caught in the HYPE of tomorrow

Alan Williamson

Subscribe to Alan Williamson: eMailAlertsEmail Alerts
Get Alan Williamson: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Apache Web Server Journal, Java Developer Magazine

Apache Web Server: Article


Java Library for building TCP servers

Recently I have been tasked with building an IMAP server.  Not a true IMAP server we are merely using IMAP as an interface to another system.  So I am developing the proxy class library which involves writing all the communications and command processing for the IMAP protocol.  I first had a look around at various projects to see if there was something I could hook into quickly.  Sadly the Apache JAMES project isn't supporting IMAP yet and even if it did, it would still be a little heavy for our requirements.  The quickest thing is to implement the handful of commands from the IMAP protocol that we needed.

IMAP works in the same manner as other TCP protocols (SMTP, POP, HTTP) in that line-terminated strings are passed back and forth with a known format so the client and server can communicate easily.  I have written many of these types of servers in the past and was looking forward to trying my hand at it again.  However on a trawl in Google for something completely different, I discovered a gem of an open-source project; QuickServer.

QuickServer is a Java library that takes all the hassle and logistics away from building TCP servers.  It is a complete framework that performs all the underlying communications and allows you to get on with implementing the commands.  It sits in one JAR and the installation took only a matter of minutes to get going.   So how does it work?  Looking at a very basic example, you define a class that implements the ClientCommandHandler class.  Each method is used for each state of the connection.  One for when the client first connects, one for when it closes, and another for when the connection is lost.  When a message is received the handleCommand() method is triggered passing in the command that was read.

public class sampleCommandHandler implements ClientCommandHandler {
  public void gotConnected(ClientHandler handler) throws SocketTimeoutException, IOException {
    handler.sendSystemMsg( "New client " + handler.getSocket().getInetAddress() );
    handler.sendClientMsg( "* OK Sample Framework $Revision: 1.5 $ ready" );

  public void lostConnection(ClientHandler handler) throws IOException {
    handler.sendSystemMsg("Connection lost : " + handler.getSocket().getInetAddress());

  public void closingConnection(ClientHandler handler)throws IOException {
    handler.sendSystemMsg("Connection closed : "+handler.getSocket().getInetAddress());

  public void handleCommand(ClientHandler handler, String command)
                            throws SocketTimeoutException, IOException {
    if ( command.getCommand().equals("LOGIN") ){
      processingLOGIN( handler );
      handler.sendClientMsg( " BAD Command unknown " + command );

It is a wonderfully easy and robust library to be using.  The example here doesn't do it justice, but in the sample applications that come with it, is a full FTP server.  The library also lets you control the total number of concurrent client connections, logging facilities and various other nuggets. The only niggle I have is that while you can easily log everything the server receives, you can't log data leaving the server.  A slight omission to what is otherwise a very complete package.  The library is released under the Lesser-General Public License so you can easily use it any of your projects.

If you need to build a TCP server (and let's face it, it is very rare these days you have to) then check out QuickServer.  It is well worth it.

Republished from Alan Williamson's blog

More Stories By Alan Williamson

Alan Williamson is widely recognized as an early expert on Cloud Computing, he is Co-Founder of aw2.0 Ltd, a software company specializing in deploying software solutions within Cloud networks. Alan is a Sun Java Champion and creator of OpenBlueDragon (an open source Java CFML runtime engine). With many books, articles and speaking engagements under his belt, Alan likes to talk passionately about what can be done TODAY and not get caught up in the marketing hype of TOMORROW. Follow his blog, or e-mail him at cloud(at)

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.