我输入了"bye"并不能结束会话,怎么回事?
在客户端的控制台输入了“bye”,服务器的控制台并没有打印exceptionCaught()方法和sessionClosed()方法,说明没有结束掉会话,这个是怎么回事呢?
客户端代码:
public void start(){
BufferedReader inputReader = null; //这个用于从控制台读取数据
BufferedReader socketReader = null; //这个用于从Socket读取数据
BufferedWriter writer = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1",9898);
//从socket读取流,也就是读取从服务器返回的数据
socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获取流
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//表示从控制台输入
inputReader = new BufferedReader(new InputStreamReader(System.in));
//启动线程
startServerReplyListener(socketReader);
//从控制台读取输入的内容
String inputContent;
int count = 0;
while(!(inputContent=inputReader.readLine()).equals("bye")){
//System.out.println(inputContent);
writer.write(inputContent);
if(count%2 == 0){
writer.write("\n");
}
count++;
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally{ //关闭
try {
socketReader.close();
writer.close();
inputReader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 开启一个接收服务器消息的监听器
*/
public void startServerReplyListener(final BufferedReader reader){
new Thread(new Runnable() {
@Override
public void run() {
try {
String response;
while((response=reader.readLine())!=null){
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}