Link to home
Start Free TrialLog in
Avatar of dpalyca755
dpalyca755

asked on

java sockets

When reading from a socket?  How can I know on the client side that the server has closed the connection?  I am looking for a condition to test, to know whether to continue reading or not.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need to test the error flag in fact, like this :

try{
 while(presentlyconnected){//*****maintain connection*****
        PW_printwriterclient.println("noOp");
	         Thread.yield(); // stop CPU burning.	
		         if(PW_printwriterclient.checkError()){presentlyconnected=disconnekt();}
			     }
				 }catch(Exception intsoc){intsoc.printStackTrace();}

Open in new window


This is working code from a prog of mine; so "diskonnekt()" is a function which closes the sockets and does some cleanup. The boolean of the while loop is set initially elsewhere, and when it becomes false, not only terminates this method, but the entire app's connections.

checkError() checks a flag, rather than catch an Exception.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpalyca755
dpalyca755

ASKER

So to clarify more I am working with 2 different connection types:

1.  A Persistent Connection.
             The only time I would expect this connect to break is if the server dies,
             in which case I guess will know by the exception getting thrown when trying to read.
2.  A Transient Connection.
             Since this is a limited dataset, should the server somehow let me know it is done sending data prior to closing the connection?  What is the normal industry paradigm?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial