I have a listening port on my server that I’m connecting to using a Java class and the Socket interface, i.e.
Socket mySocket = new Socket(host,port);
I then grab an OutputStream, decorate with a PrintWriter in autoflush mode and I’m laughing – except if the listening port closes. Then I get
tcp4 0 0 *.9999 *.* LISTEN tcp 0 0 127.0.0.1.45737 127.0.0.1.9999 CLOSE_WAIT
and I can’t seem to detect the problem in the program – I’ve tried using the isConnected() method on the socket but it doesn’t seem to know that the connection is closed.
I want to be aware of the problem the next time I try and write to the Socket so that I can try and reconnect and report the issue.
Any advice please?
Thanks all
Set a short timeout?
Does
isOutputShutdown()not get you what you want?You could always build a
SocketWatcherclass that spins up in its ownThreadand repeatedly tries to write empty strings to theSocketuntil that raises aSocketClosedException.