Client Termination With Java Sockets doesnt close socket

J

Jae

Hello, i have a simple client/server app using java sockets. If i
simply close the client window, the server doesnt detect if the socket
is closed. Here is all the ways ive tried to test for the closed
connection but none work.

----------------------------------------------------------------------

if(soc.isClosed() || !soc.isConnected() || soc.isInputShutdown()||
soc.isOutputShutdown()){
//socket is closed
}

----------------------------------------------------------------------

then i tried this:
--------------------------------------------------------------
try{
PrintWriter br = new PrintWriter(soc.getOutputStream(), true);
br.println("d");
}catch(IOException ie){

//socket is closed
}
 
T

Thomas Fritsch

Jae said:
Hello, i have a simple client/server app using java sockets. If i
simply close the client window, the server doesnt detect if the socket
is closed. Here is all the ways ive tried to test for the closed
connection but none work.

----------------------------------------------------------------------

if(soc.isClosed() || !soc.isConnected() || soc.isInputShutdown()||
soc.isOutputShutdown()){
//socket is closed
}

----------------------------------------------------------------------

then i tried this:
--------------------------------------------------------------
try{
PrintWriter br = new PrintWriter(soc.getOutputStream(), true);
br.println("d");
}catch(IOException ie){

//socket is closed
What makes you think, the socket is closed here?
Did you call
soc.close();
anywhere in your code?
 
J

Jae

well on abnormal program termination of the client there is no chance
to call soc.close(). I tried putting it in the destructor but that
doesnt work. I would expect that when the client is closed the socket
would close automatically...
 
S

Steve Horsley

Jae said:
Hello, i have a simple client/server app using java sockets. If i
simply close the client window, the server doesnt detect if the socket
is closed.

In java, closing the window doesn't necessarily terminate the
application. It may simply be hiding the window and leaving the
client app running. Check this.
Here is all the ways ive tried to test for the closed
connection but none work.

----------------------------------------------------------------------

if(soc.isClosed() || !soc.isConnected() || soc.isInputShutdown()||
soc.isOutputShutdown()){
//socket is closed
}

isClosed() will not return true unless YOU close the socket. A
close by the far end will not affect that, so this approach is a
dead-end.
----------------------------------------------------------------------

then i tried this:
--------------------------------------------------------------
try{
PrintWriter br = new PrintWriter(soc.getOutputStream(), true);
br.println("d");
}catch(IOException ie){

//socket is closed
}

You should br.flush() to make sure the message actually gets
sent. Also, I _suspect_ that you would need to send something
else, later, after the first message has gone un-acknowleged for
several minutes, and that the second message (after TCP has given
up trying to deliver the first message) that you will get the
exception.

So, really, application keepalive messages and inactivity timers
is probably the best way to go.

Steve
 
J

Jae

oh i should have mentioned, when i said window i meant command window,
not gui window.. the app is definitely closing

also, i had that that try/catch block in a loop, so it send 100s of
msgs and it never through the exception. I find it odd that it was
sending messages to a client that wasnt running, and no excpetion that
was thrown.

Could this be a bug in the java TCP/IP implementation?
 
T

Thomas Fritsch

Jae said:
well on abnormal program termination of the client there is no chance
to call soc.close(). I tried putting it in the destructor but that
doesnt work. I would expect that when the client is closed the socket
would close automatically...
May be I misunderstood your original message. I assumed your posted code was
*client* code, and gave my answer based on this assumption. If it is
*server* code instead, my answer makes no sense; just disregard it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top