How do I check if output stream of a socket is closed?
- by Roman
I have this code:
public void post(String message) {
output.close();
final String mess = message;
(new Thread() {
public void run() {
while (true) {
try {
output.println(mess);
System.out.println("The following message was successfully sent:");
System.out.println(mess);
break;
} catch (NullPointerException e) {
try {Thread.sleep(1000);} catch (InterruptedException ie) {}
}
}
}
}).start();
}
As you can see I close the socket in the very beginning of the code and then try to use it to send some information to another computer. The program writes me "The following message was successfully sent". It means that the NullPointerException was not thrown.
So, does Java throw no exception if it tries to use a closed output stream of a socket? Is there a way to check if a socket is closed or opened?
ADDED
I initialize the socket in the following way:
clientSideSocket = new Socket(hostname,port);
PrintWriter out = new PrintWriter(clientSideSocket.getOutputStream(), true);
browser.output = out;