我有一个服务器端的问题,当我特意拔掉android wifi服务器无法检测到通信结束。如何发现客户端android socket断开连接?
当关闭应用程序服务器时发现。当客户端断开连接,但是当我关掉了wifi的机器人,该方法被搁置
/**
* Method Responsible for receiving last message from client socket.
*
* @return
* @throws java.io.IOException
*/
public String receive() throws IOException {
//receiver is a Scanner -> receiver = new Scanner(socket.getInputStream());
while (receiver.hasNextLine()) {
return receiver.nextLine();
}
return null;
}
@Override
public void run() {
//loop waiting for the client message
while (true) {
String response;
//receive message
try {
response = receive();
//if the message come null means that the client disconnected
if (response == null) {
break;
}
} catch (Exception e) {
break;
}
//mount message.
Message msg;
try {
msg = new Gson().fromJson(response, Message.class);
} catch (Exception e) {
continue;
}
//manage message in new thread
new Thread(new Runnable() {
@Override
public void run() {
manageMessage(msg);
}
}).start();
}
close();
}
的方法总是返回null。