关于QuickServer在客户端编程如下:
客户端相关的业务类:
package com.easway.quickserver.echo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 客户端业务类的实现
* @author longgangbai
*
*/
public class BussineClient {
private static final Log log = LogFactory.getLog(BussineClient.class);
private Socket socket;
private BufferedReader in;
private int threadNum;
private PrintWriter out;
public BufferedReader getBufferedReader() throws IOException{
Reader inputStream =new InputStreamReader(socket.getInputStream());
return new BufferedReader(inputStream);
}
public PrintWriter getPrintWriter() throws IOException{
OutputStream outputStream =socket.getOutputStream();
return new PrintWriter(outputStream,true);
}
public BussineClient(int num){
this.threadNum=num;
//执行相关的业务
action();
}
/**
* 执行相关的业务的方法的信息
*/
public void action(){
try
{
socket = new Socket("localhost", 4123);
in = getBufferedReader();
out = getPrintWriter();
//写入线程编号
out.println("############ ttt 000"+this.threadNum);
String line=null;
while((line=in.readLine())!=null){
System.out.println("line ="+line);
}
out.close();
in.close();
socket.close();
}
catch (IOException e){
e.printStackTrace();
System.exit(1);
}
}
}
客户端执行任务的线程:
package com.easway.quickserver.echo;
/**
* 执行业务的线程
* @author longgangbai
*
*/
public class BussineClientThread implements Runnable{
private int threadNum;
private BussineClient client;
public BussineClientThread(int threadNum){
this.threadNum=threadNum;
}
@Override
public void run() {
client=new BussineClient(threadNum);
}
}
客户端测试的代码:
package com.easway.quickserver.echo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/***
* 测试业务客户端的服务
* @author longgangbai
*
*/
public class BussineClientTest {
private static final Log log = LogFactory.getLog(BussineClientTest.class);
public static void main(String[] args) {
for(int i=0;i<10;i++){
log.info("Enter run l..."+i);
new Thread(new BussineClientThread(i)).start();
}
}
}