一个启动供手机客户端访问服务端的简例

package com.jh.jcs.mobile.android.servlet;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
import edu.emory.mathcs.backport.java.util.concurrent.Executors;

public class AndroidServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**
* 日志对象
*/
Log logger = LogFactory.getLog(AndroidServlet.class);

public AndroidServlet(){
super();
}

public void destroy() {
super.destroy();
}

@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
try {
/*Thread startAndroid = new AndroidServerThread();
startAndroid.start();*/

//jiangkun 改为多端口监听
new AndroidServerThread();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.jh.jcs.mobile.android.servlet;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jh.jcs.framework.utils.ResourceServiceConfig;

import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
import edu.emory.mathcs.backport.java.util.concurrent.Executors;

public class AndroidServerThread{

Log logger = LogFactory.getLog(AndroidServerThread.class);

//private int port=8831;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小

public AndroidServerThread() throws IOException{
int port1 = Integer.parseInt(ResourceServiceConfig.getProperties().getProperty("android_port1"));
int port2 = Integer.parseInt(ResourceServiceConfig.getProperties().getProperty("android_port2"));
new download(port1).start();
new download(port2).start();
//serverSocket = new ServerSocket(port);

//Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
logger.info("Android服务启动!");
}
class download extends Thread{

private ServerSocket serverSocket;
private int port;
public download(int port) {
// TODO Auto-generated constructor stub
this.port = port;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
Socket socket=null;
try {
socket = serverSocket.accept();
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket.setSoTimeout(2000);
logger.info("客户发来请求!");
int port1 = Integer.parseInt(ResourceServiceConfig.getProperties().getProperty("android_port1"));
if (serverSocket.getLocalPort()==port1) {
executorService.execute(new HandlerHelper(socket));
} else {
executorService.execute(new UploadHelper(socket));
}

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

}

}

你可能感兴趣的:(客户端)