How to find out socket connection status inside PrintWriter

I use

PrintWriter output = new PrintWriter(socket.getOutputStream(), true);

......
output.println(message);
......

Since the println function has no return value, it also does not raise any exceptions. How can I find out that disconnection is disabled?

thank

+3
source share
2 answers

The call PrintWriter.checkError()will tell you if any error has occurred. It does not specifically indicate which error occurred, but if checkError returns true, you can conclude that the most likely reason is a closed or broken socket.

If you keep a reference to the socket from which you receive the input stream, you can check the status of the socket.

, , " ", OutputStream, IOException ( ..), , - ,

+1

. http://download.oracle.com/javase/6/docs/api/java/net/Socket.html

boolean isClosed(), .

boolean isConnected(), .

+1

Source: https://habr.com/ru/post/1789366/


All Articles