I know that there have been quite similar questions in the past and I studied most of them but couldn't manage to get my code working.
I have a TCPIP-Server written in Java and a client Android app.
When I am turning off my WLAN on my phone I obviously don't have any connection to my server but the server doesn't notice this.
How I read on the server
try {
bufferSender = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//this while it's like a listener for messages
String message = null;
while (running) {
try {
message = in.readLine();
if (message == null)
System.out.println(this.user.getUsername() + " left the room.");
} catch (IOException e) {
System.out.println("Error reading message: " + e.getMessage());
running = false;
managerDelegate.userDisconnected(this);
}
if (message != null && managerDelegate != null) {
user.setMessage(message);
System.out.println("message:" + message);
// notify message received action
managerDelegate.messageReceived(user);
}
}
} catch (Exception e) {
System.out.println("S: Error");
e.printStackTrace();
}
I have read that I should get an error when trying to read a Message from the Client. But I don't get any error.
So I tried to send a message from the Server to the Client every X seconds. This is how I send it:
public void sendMessage(String message) {
if (bufferSender != null && !bufferSender.checkError()) {
bufferSender.println(message);
bufferSender.flush();
} else {
System.out.println("ERROR");
}
}
But again, nothing happens. I don't get any error by sending a message to the client even if the client has no connection to the internet.
What am I doing wrong?