我的客户端需要与一个服务器进行通讯,保持长连接,不知道服务器为何种类型,服务器只提供IP地址和端口号,我的客户端要经常向服务器发送数据,定期还要接受服务器发来的命令,要求我的客户端具有当连接断开的时候自动重连的功能,现在的问题是,我的客户端在和服务器建立连接之后,时间长了,连接会消失,我的客户端运行在solaris系统下,比如说服务器监听的端口是7010,那么我运行netstat -a | grep 7010,则什么也看不到了,而建立连接时可以看到状态是established的,可能的原因是服务器重启或者关机造成的,但是我的重新连接的机制没有发挥作用,而我在局域网测试,当服务器关机重启,或者断开连接,重新监听,我的客户端是可以重新连接上的,我采用的检测连接状态的办法是捕获输入流的异常,如果输入流抛出异常就关闭socket,然后重新连接,程序如下:高手看看,为什么当服务器不在局域网的时候,这种重新连接的机制不起作用?public final class NafClient implements MsgDef {
public Socket tonaf = null;// 连接服务器的socketpublic String nafip;// 服务器的IPpublic int port;// 服务器的portpublic int bprimary;// 该client连接的是否是服务器public PrintWriter out;// 用于输出public BufferedReader in;public NafProc proc = null;public ConfigData cfgData = null;public int nafType;/***************************************************************************
 * 构造函数
 * 
 * @param ip
 * @param i
 * @param j
 * @param proc
 */
public NafClient(String ip, int i, int j, int type, NafProc proc) {
this.nafip = ip;
this.port = i;
this.bprimary = j;
this.cfgData = ConfigData.Instance();
this.nafType = type;
this.proc = proc;
new ClientLinkHolder().start();
}/***************************************************************************
 * 连接服务器
 */
public final boolean connectToNaf() {
try {
tonaf = new Socket(nafip, port);
out = new PrintWriter(tonaf.getOutputStream());
in = new BufferedReader(new InputStreamReader(tonaf
.getInputStream()));
proc.onLinkConnectOk(this);
DMSG.info("connected to naf!");
return true;
} catch (UnknownHostException e) {
return false;
} catch (Exception e) {
DMSG.info("error when connect to naf" + nafip + ":", e);
return false;
}
}/***************************************************************************
 * 向服务器发送告警
 */
public final synchronized void sendAlarm(String alarm) {
out.print(alarm);
out.flush();
}/***************************************************************************
 * 判断与服务器的连接状态
 */
public final synchronized boolean isConnected() {
if (this.tonaf == null)
return false;
if (this.tonaf.isConnected() == true)
return true;
else
return false;
}/***************************************************************************
 * 如果没有连接通知NafProc
 */
public final void isNotConnected() {
proc.onLinkDisConnect(this);
}/***************************************************************************
 * 内部类,NafClient的保持连接线程
 */
public final class ClientLinkHolder extends Thread {
/***********************************************************************
 * 从收到的数据中解析出同步请求
 * 
 * @param req
 */
public String solveRecvData(char[] req) {
String data = String.copyValueOf(req);
int index1 = 0, index2 = 0;
index1 = data.indexOf(cfgData.NAFSyncReq);
index2 = data.lastIndexOf(cfgData.NAFSyncEndFlag);
String str = null;
if (index1 != -1)
if (index2 != -1) {
str = data.substring(index1, index2);
}
return str;
}public void run() {
while (true) {
if (!isConnected()) {
if (connectToNaf() == false) {
try {
Thread.sleep(10000);
} catch (Exception e) {
}
}
} else {
while (true) {
try {
char[] req = new char[SYNCBUFFERSIZE];
req[SYNCBUFFERSIZE - 1] = '\0';
in.read(req);
String cmd = null;
cmd = solveRecvData(req);
if (cmd != null) {
DMSG.info("receive resend command!" + cmd);
proc.onResendReq(req);
}
} catch (Exception e) {
DMSG.info("lose connection to naf " + nafip + ":",e);
isNotConnected();
try {
in.close();
in = null;
tonaf.close();
tonaf = null;
} catch (IOException e1) {
}
break;
}
}
}
}
  }
}       /***************************************************************************
 * 
 * @return
 */
public NafProc getProc() {
return proc;
}/***************************************************************************
 * 
 * @param proc
 */
public void setProc(NafProc proc) {
this.proc = proc;
}private final static DebugPrn DMSG = new DebugPrn(NafClient.class.getName());
}