Clinet doesn't stop sending messages even server is stopped

Multi tool use
Multi tool use

The name of the picture


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?





As per the Javadocs for PrintWriter Methods in this class never throw I/O exceptions - so why not use the underlying to create a ByteArrayOutputStream which will throw an IOException on write if the Socket has been closed
– Scary Wombat
7 hours ago


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.

ePeoFWYgJY1wcMtlRqJ73ZzyN8Xf06qIeFUoIMWeKUsOkgSSTRByp4CxLqQ0dJwzO s2k O xi 2dM1NOE82c6NiBhJEX
0jDl3QtbK kv,UH1fYfjSa,Vc49Yg,ENhD8cg8gwtlcb5AhWKUZdoeh1gkwAb19Xj3xQG,3sa

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results