我是套接字编程的新手,并且有一段代码可以打开一个套接字并写入它。我将套接字的超时设置为一分钟,并希望在达到一定条件后关闭套接字并退出。
满足条件时,我的代码没有关闭套接字:
@Override
public void run() {
Socket socket =null;
PrintWriter writer = null;
BufferedReader reader = null;
String host = ServiceProperties.getInstance().getControllerHost();
String port = "1234;
String info="";
// TODO Auto-generated method stub
try {
socket = new Socket(host, Integer.valueOf(port));
socket.setSoTimeout(60000);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
SampleBean sBean = (SampleBean) (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sampleBean"));
info = ControllerDAO.getInstance().getControllerAndTimeScheduleInfo(sBean.getId());
writer.println("set TimeSchedule "+ info +" value ["+str+"]");
}
catch(UnknownHostException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
String line="";
try {
System.out.println("BEFORE WHILE");
System.out.println(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));
while((line= reader.readLine())!=null ) {
System.out.println(line);
if(line.contains("OK")){
System.out.println("line contains OK ");
break;
}
try {
Thread.sleep(5000);
}
catch(InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("AFTER WHILE");
System.out.println(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));
}
catch(IOException ex) {
ex.printStackTrace();
}
try {
writer.close();
reader.close();
socket.close();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
});
thread.run();
输出:
//"BEFORE WHILE"
// 14:54:55
// prints line
// //prints empty line
// now it waits for like 40 seconds
// line contains OK //condition met here
// breakoutof the loop
// "AFTER WHILE"
// 14:55:55
为什么要等待第三次迭代?第三次迭代是当条件满足时,等待大约 40 秒后。
我究竟做错了什么?