入口文件: Receive.java
package main;
import org.quickserver.net.AppException;
import org.quickserver.net.server.QuickServer;
public class Receive {
public static void main(String s[]) {
QuickServer myServer =
new QuickServer("main.EchoCommandHandler");//其中main是包名
myServer.setPort(7412);
myServer.setName("EchoServer v 1.0");
try {
myServer.startServer();
} catch(AppException e){
System.err.println("Error in server : "+e);
}
}
}
Hander 文件 EchoCommandHandler.java
package main;
import java.net.*;
import java.io.*;
import org.quickserver.net.server.ClientCommandHandler;
import org.quickserver.net.server.ClientHandler;
public class EchoCommandHandler implements ClientCommandHandler {
public void gotConnected(ClientHandler handler)
throws SocketTimeoutException, IOException {
handler.sendClientMsg("Connection opened : "+
handler.getSocket().getInetAddress());
handler.sendClientMsg("Welcome to EchoServer v1.0 ");
handler.sendClientMsg("Note: Password = Username");
handler.sendClientMsg("Send 'Quit' to exit");
}
public void lostConnection(ClientHandler handler)
throws IOException {
handler.sendSystemMsg("Connection lost : " +
handler.getSocket().getInetAddress());
}
public void closingConnection(ClientHandler handler)
throws IOException {
handler.sendSystemMsg("Connection closing : " +
handler.getSocket().getInetAddress());
}
public void handleCommand(ClientHandler handler, String command)
throws SocketTimeoutException, IOException {
handler.sendClientMsg("+++++++++++++++++++++++++++++++");
//显示客户端接收到的信息
System.out.println("command="+command);
if(command.toLowerCase().equals("quit")) {
handler.sendClientMsg("Bye ;-)");
handler.closeConnection();
} else {
handler.sendClientMsg("Echo : " + command);
}
}
}
然后将 附件 LIB 文件夹下的 .jar 导入项目
org.apache 和 org.quickserver 可用于CMD 直接编译
2.0 版本 可设置 服务器 TIMEOUT
Receive.java
package main;
import org.quickserver.net.AppException;
import org.quickserver.net.server.QuickServer;
import org.quickserver.util.xmlreader.QuickServerConfig;
import org.quickserver.util.xmlreader.Secure;
public class Receive {
public static void main(String s[]) {
QuickServer echoServer = new QuickServer();
try {
QuickServerConfig cfg = new QuickServerConfig();
cfg.setClientCommandHandler("main.EchoCommandHandler");
cfg.setPort(2899);
cfg.setName("EchoServer");
cfg.setTimeout(5000);
//必须绑定本地地址
//cfg.setBindAddr("192.168.1.102");
cfg.setBindAddr("localhost");
echoServer.initServer(cfg);
echoServer.startServer();
} catch (AppException e) {
System.out.println("Error in server : " + e);
e.printStackTrace();
}
}
}
请看 LIB2.0