匿名用户
1级
2013-11-20 回答
socket.setKeepAlive()应该写在客户端,server.accept()是服务器端的。
心跳的也有现成的,不过依赖别的类。大概贴一下,就是这么个意思。
public class HBHBThread extends Thread {
private CommandDeal command;
/**
* @return the command
*/
public CommandDeal getCommand() {
return command;
}
/**
* @param command
* the command to set
*/
public void setCommand(CommandDeal command) {
this.command = command;
}
public void run() {
long maxWait = SendConfig.hbhb_wait;
if (maxWait
maxWait = 1000;
}
Date lastTime;
long timeDiv=0;
while (1 == 1) {
lastTime=SocketQueueObject.getLastContactTime();
if(lastTime!=null){
timeDiv=(new Date()).getTime() - SocketQueueObject.getLastContactTime().getTime();
System.out.println("TimeDiv:"+timeDiv);
}
// 上次联系的时间超过最大等待,或者还没联系过
if (lastTime == null|| timeDiv >=maxWait) {
System.out.println("HBHB");
command.sendHbhbCommand();
}else if(maxWait>timeDiv){
maxWait=maxWait-timeDiv;
}
try{
this.sleep(maxWait);
}catch(Exception e){
e.printStackTrace();
}
}
}public class HBHBCommandDeal extends CommandDealInterface {
private static Logger msg_logger = Logger.getLogger("socketMsg");
@SuppressWarnings("unchecked")
public List doCommand(SocketQueueObject socketQueueObject, Map command) throws Exception {
List commandList = (List) command.get(Constants.SMSCOMMAND);
byte[] hbhbMsg = TL1Tool.createCommand(commandList.get(0));
msg_logger.info(" send message to sms packed:" + new String(hbhbMsg, "ISO-8859-1"));
TL1RetrunMessage retMsg = socketQueueObject.commNGNServer(hbhbMsg);
retMsg.resolve();
msg_logger.info(" recevice message from sms:"+ retMsg.getMessage());
List returnList = new ArrayList();
returnList.add(retMsg);
return returnList;
}
}你可以自己找找看啊。