Quick Server example with Eclipse IDE.

First of all include jar file in your java project.

Let’s create main class to listen on specific port.

 

import java.io.File;

import java.io.IOException;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.logging.FileHandler;

import java.util.logging.Level;

import java.util.logging.Logger;

import java.util.logging.SimpleFormatter;

 

import org.quickserver.net.AppException;

import org.quickserver.net.server.DataMode;

import org.quickserver.net.server.DataType;

import org.quickserver.net.server.QuickServer;

 

public class Server {

public static void main(String s[])  {

QuickServer quickServer = new QuickServer(“com.quick.CommandHandler”);

quickServer.setPort(2500);

quickServer.setTimeout(10000000);

quickServer.setName(“Quick Server”);

Logger logger = null;

FileHandler xmlLog = null;

FileHandler txtLog = null;

File log = new File(“./log/”);

if(!log.canRead()){

log.mkdir();

}

try {

DateFormat dateFormat = new SimpleDateFormat(“dd-MM-yyyy”);

logger = Logger.getLogger(“”);

logger.setLevel(Level.INFO);

xmlLog = new FileHandler(“log/QuickServer-“+dateFormat.format(new Date())+”.xml”,true);

logger.addHandler(xmlLog);

logger = Logger.getLogger(“com.quick”);

logger.setLevel(Level.INFO);

txtLog = new FileHandler(“log/QuickServer-“+dateFormat.format(new Date())+”.log”,true);

txtLog.setFormatter(new SimpleFormatter());

logger.addHandler(txtLog);

 

quickServer.setAppLogger(logger);

}

catch(IOException e) {

System.err.println(“Could not create xmlLog FileHandler : “+e);

}

try {

quickServer.startServer();

}

catch(AppException e) {

System.err.println(“Error in server : “+e);

}

catch (IOException e) {

System.err.println(“Error in server : “+e);

}

}

}

 

 

 

In the above example we can see that server will listen on 2500 port, you can change if you want. If any client will send data on this port then handleCommand method will call every time whenever this port will receive any data.

 

Below is the CommandHandler class which contains many methods.

 

package com.quick;

 

import java.net.*;

import java.util.logging.Logger;

import java.io.*;

 

import org.quickserver.net.server.ClientCommandHandler;

import org.quickserver.net.server.ClientHandler;

import org.quickserver.net.server.DataMode;

import org.quickserver.net.server.DataType;

 

public class CommandHandler implements ClientCommandHandler{

private final static Logger logger = Logger.getLogger(CommandHandler.class.getName());

public void gotConnected(ClientHandler handler) throws SocketTimeoutException, IOException {

handler.sendClientMsg(“*******************************”);

handler.sendClientMsg(“*   Welcome to Quick Server   *”);

handler.sendClientMsg(“*     Author Manoj Bhakar     *”);

handler.sendClientMsg(“*     Send ‘Quit’ to exit     *”);

handler.sendClientMsg(“*******************************”);

}

public void lostConnection(ClientHandler handler) throws IOException {

handler.sendSystemMsg(“Connection lost : ” + handler.getSocket().getInetAddress());

}

public void closingConnection(ClientHandler handler) throws IOException {

handler.sendSystemMsg(“Closing connection : ” + handler.getSocket().getInetAddress());

}

 

public void handleCommand(ClientHandler handler, String command) throws SocketTimeoutException, IOException {

if(command.equalsIgnoreCase(“Quit”)) {

handler.sendClientMsg(“Bye ;-)”);

handler.closeConnection();

}

else {

System.out.println(“Received Command : ”+command);

}

}

 

 

gotConnected method will be called when Server get connected. And this method will send messages to the clients when clients get connected to the server.

 

handleCommand method will be used to handle the String that this port will receive from clients.