下面编写一个最简单的QuickServer实现如下:
服务端信息处理类:
package com.easway.quickserver.echo;
import java.io.IOException;
import java.net.SocketTimeoutException;
import org.quickserver.net.server.ClientCommandHandler;
import org.quickserver.net.server.ClientEventHandler;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.net.server.DataMode;
import org.quickserver.net.server.DataType;
/**
* 服务端业务处理类的面向所有客户端做命令处理器,实现了ClientCommandHandler接口。
*
*
*
* 在客户端必须实现是ClientCommandHandler类用于处理客户端的各种请求的各种信息
* 实现ClientEventHandler用于响应客户端的连接的各种对应的事件信息
*
* @author longgangbai
*
*/
public class EchoCommandHandler implements ClientCommandHandler ,ClientEventHandler
{
/**
* 连接服务时响应相关的信息
*/
public void gotConnected(ClientHandler handler)
throws SocketTimeoutException, IOException {
//设置请求和响应的数据类型的信息
handler.setDataMode(DataMode.STRING, DataType.IN);
handler.setDataMode(DataMode.STRING, DataType.OUT);
//设置相关的连接成功的信息
handler.sendClientMsg("+++++++++++++++++++++++++++++++");
handler.sendClientMsg("| Welcome to EchoServer v 1.0 |");
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());
}
/**
* 此方法为ClientCommandHandler接口的核心方法,因为服务器接受客户端发送的任何命令时
* 都要盗用该方法,
* 处理客户端的请求的信息
*
*/
public void handleCommand(ClientHandler handler, String command)
throws SocketTimeoutException, IOException {
handler.sendClientMsg("+++++++++++++++++++++++++++++++");
//显示客户端接收到的信息
System.out.println("command="+command);
//根据相关的信息返回相关的信息
if(command.equals("Quit")) { //关闭相关的服务
handler.sendClientMsg("Bye ;-)");
handler.closeConnection();
} else { //向客户端的信息
handler.sendClientMsg("server Echo : "+command);
}
}
}
服务端编程实现如下:
package com.easway.quickserver.echo;
import org.quickserver.net.*;
import org.quickserver.net.server.*;
/**
* QuickServer 服务端启动的编程时实现
*
* @author longgangbai
*
*/
public class EchoServer {
public static void main(String s[]) {
QuickServer myServer =
new QuickServer("com.easway.quickserver.echo.EchoCommandHandler");
myServer.setPort(4123);
myServer.setName("EchoServer v 1.0");
try {
myServer.startServer();
} catch(AppException e){
System.err.println("Error in server : "+e);
}
}
}
声明式编程的实现:
import java.io.File;
import org.quickserver.net.AppException;
import org.quickserver.net.server.QuickServer;
/**
* QuickServer 服务端启动的编程时实现
*
* @author longgangbai
*
*/
public class EasyWayServer {
public static String VER = "0.1";
public static void main(String[] args) {
QuickServer gateWayServer = new QuickServer();
String confFile = "config"+File.separator+"EchoServer.xml";
Object config[] = new Object[] {confFile};
try {
if(gateWayServer.initService(config) == true) {
gateWayServer.startServer();
}
} catch(AppException e) {
System.out.println("服务器错误 : "+e);
e.printStackTrace();
}
}
}
服务器端配置如下:
EchoServer.xml如下:
<quickserver>
<name>EchoServer v 1.0</name>
<port>4123</port>
<bind-address>127.0.0.1</bind-address>
<client-command-handler>
com.easway.quickserver.echo.EchoCommandHandler
</client-command-handler>
</quickserver>