對於併發量不是很大的基本可以滿足要求,如果是高併發則需要“多線程”,可以使用線程池。
SocketServer
package cn.com.ccb.inface.server;
import java.net.ServerSocket;
import cn.ccb.iplat.controller.soap.IECDSService;
import com.tansun.framework.war.zbill.accptnc.dao.IAccptncDAO;
public class SocketServer {
IAccptncDAO accptncZillDAO;
IECDSService ecdsService;
public IAccptncDAO getAccptncZillDAO() {
return accptncZillDAO;
}
public void setAccptncZillDAO(IAccptncDAO accptncZillDAO,IECDSService ecdsService) {
this.accptncZillDAO = accptncZillDAO;
this.ecdsService=ecdsService;
}
/**
* 信貸接口
*/
public void initServer()
{
int port = 8898;
try {
ServerSocket server = new ServerSocket(port);
new Thread(new Task(server,accptncZillDAO,ecdsService)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
public IECDSService getEcdsService() {
return ecdsService;
}
public void setEcdsService(IECDSService ecdsService) {
this.ecdsService = ecdsService;
}
public void setAccptncZillDAO(IAccptncDAO accptncZillDAO) {
this.accptncZillDAO = accptncZillDAO;
}
}
===================task
package cn.com.ccb.inface.server;
import java.net.ServerSocket;
import java.net.Socket;
import cn.ccb.iplat.controller.soap.IECDSService;
import com.tansun.framework.war.zbill.accptnc.dao.IAccptncDAO;
public class Task implements Runnable {
private ServerSocket server;
private IAccptncDAO accptncZillDAO;
private IECDSService ecdsService;
public Task(ServerSocket server,IAccptncDAO accptncZillDAO,IECDSService ecdsService) {
this.accptncZillDAO=accptncZillDAO;
this.server=server;
this.ecdsService=ecdsService;
}
public void run(){
try {
while (true) {
//server嘗試接收其他Socket的連接請求,server的accept方法是阻塞式的
Socket socket = server.accept();
//每接收到一個Socket就建立一個新的線程來處理它
new Thread(new SocketTask(socket,accptncZillDAO,ecdsService)).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
socketTask
package cn.com.ccb.inface.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import com.tansun.framework.war.zbill.accptnc.dao.IAccptncDAO;
import cn.ccb.iplat.controller.soap.IECDSService;
import cn.com.ccb.inface.obj.XmlCfg;
import cn.com.ccb.dayend.common.IntManageLog;;
public class SocketTask implements Runnable {
private Socket socket;
private IAccptncDAO accptncZillDAO;
private IECDSService ecdsService;
public SocketTask(Socket socket,IAccptncDAO accptncZillDAO,IECDSService ecdsService) {
this.socket=socket;
this.accptncZillDAO=accptncZillDAO;
this.ecdsService=ecdsService;
}
public void run(){
OutputStream out =null;
InputStream is = null;
try {
byte[] head = new byte[8];
is = socket.getInputStream();
is.read(head);
int bodyLength = Integer.parseInt(new String(head,"gbk"));
byte[] body = new byte[bodyLength];
is.read(body);
System.out.println(new String(body,"gbk"));
byte[] xml=ServerParse.parseXc(ServerParse.parseRequest(body),accptncZillDAO,ecdsService);
System.out.println(new String(xml,"gbk"));
out = socket.getOutputStream();
out.write(xml);
out.flush() ;
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
is.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}