我的目的是:与远端服务建立好链接后,向其发送读取请求,服务端将查询结果返回给我的客户端,如果有满足条件的信息,客户端再次发送另一个请求后,再进行处理,现在的问题是我的第一次请求可以得到返回结果,但是第二次发送请求后,我的客户端接收不到数据了。这是什么原因,下面是我的程序,有什么错误?
BufferedReader in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
PrintWriter out = new PrintWriter(cSocket.getOutputStream(),true);
//发送给远端处理
out.print(sendCmd);
out.flush();
//接收返回信息
retVal = in.readLine();
getVal = retVal;
//处理返回码
reCode = retVal.substring(5,7);//提取返回信息码;
retVal=proReCode(reCode);
if(retVal.equals("0"))//如查有欠费,查询上月和前月
{
retMsg1 = proInfo(getVal);
long lFlowNo1=time.getTime();
flowNo = String.valueOf(lFlowNo1).substring(3,11);
String sendCmd1=len+tranCode+bankNo+operId+flowNo+userNo+mon1;
out.print(sendCmd1);
out.flush();
reCode = in.readLine();//接收内容,为空
tmp=retMsg1;
}
else
{
tmp=retVal;
}
BufferedReader in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
PrintWriter out = new PrintWriter(cSocket.getOutputStream(),true);
//发送给远端处理
out.print(sendCmd);
out.flush();
//接收返回信息
retVal = in.readLine();
getVal = retVal;
//处理返回码
reCode = retVal.substring(5,7);//提取返回信息码;
retVal=proReCode(reCode);
if(retVal.equals("0"))//如查有欠费,查询上月和前月
{
retMsg1 = proInfo(getVal);
long lFlowNo1=time.getTime();
flowNo = String.valueOf(lFlowNo1).substring(3,11);
String sendCmd1=len+tranCode+bankNo+operId+flowNo+userNo+mon1;
out.print(sendCmd1);
out.flush();
reCode = in.readLine();//接收内容,为空
tmp=retMsg1;
}
else
{
tmp=retVal;
}
可能是服务端处理一次请求后就断开了socket连接。
的代码问题
检测连接也是成功的
if(cSocket.isConnected() == true){
System.out.println("连接服务端成功");
}
else {
System.out.println("连接服务端失败");
return "-1";
}
也就是说,一旦socket已经连接,即使你调用了close(),isConnected()方法还是会返回true。