|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Product Reviews QuickServer
Java Library for building TCP servers
By: Alan Williamson
Nov. 28, 2003 12:00 AM
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 );
else
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 blogReader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||