Clinet doesn't stop sending messages even server is stopped

Multi tool use
Clinet doesn't stop sending messages even server is stopped
I am working on a Java client which has to send a heartbeat message periodically. I am using java.net.Socket for the work. My issue is even the server is stopped client goes on sending the messages without giving any exception.
I read various stackoverflow questions on this, but couldn't find the answer for my implementation. I read that only way to find the server is up is to try sending a message. But here I don't see a way to do this.
Here's how my class looks like.
public void process() {
sendMessage(HEART_BEAT);
}
public void start(String serverAddress, int port) throws IOException {
this.serverAddress = serverAddress;
this.port = port;
if (isConnected) {
LOGGER.info("Already connected");
return;
}
socket = new Socket(serverAddress, port);
out = new PrintWriter(socket.getOutputStream(), true);
}
private void sendMessage(String message) {
out.print(message + END_OF_MESSAGE);
out.println();
}
Here's how my main method looks like,
public static void main(String args) {
Client client=new Client();
client.start("127.0.0.1", 900);
while(true) {
client.process();
Thread.sleep(2000);
}
}
How to implement a way to identify when the server has stopped?
PrintWriter
ByteArrayOutputStream
write
2 Answers
2
Why do you need to send messages if the server is stopped? You can use try catch block to handle any exceptions while sending messages, but in this case messages would not be sended to the server and will wait until the server is up. Or you can check the server is running or not - How to do a true Java ping from Windows? by using isReachable method of java.net.InetAddress class.
final InetAddress host = InetAddress.getByName("IP or hostname");
System.out.println("host.isReachable(1000) = " + host.isReachable(1000)); // true - if host is alive
and after that send the message.
The PrintWriter
may not throw any exception when wirting but that doesn't mean that it hasn't run on an error. The underlying OutputStream
still throws an exception but it's internally caught and processed.
The original exception and information may be lost but the PrintWriter#checkError()
method does still provide a way to check if something went wrong.
My suggestion is, to add some lines of code to your sendMessage
method:
PrintWriter
OutputStream
PrintWriter#checkError()
sendMessage
private void sendMessage(String message) {
out.print(message + END_OF_MESSAGE);
out.println();
if ( pw.checkError() )
{
LOG.log( Level.SEVERE, "An error occured while writing.");
shutdown(); // <-- method with the shutdown logic (e.g. closing the socket).
}
}
Note that the PrintWriter#checkError()
method also flushes the stream before checking if an error occured.
PrintWriter#checkError()
It may not be the most elegant solution but it get's the job done.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
As per the Javadocs for
PrintWriter
Methods in this class never throw I/O exceptions - so why not use the underlying to create aByteArrayOutputStream
which will throw an IOException onwrite
if the Socket has been closed– Scary Wombat
7 hours ago