Socket - testing if serversocket has been closed down
My client socket calls a method 60 times a second which runs this segment of code:
if(bin.ready()){
input = bin.readLine();
//do something
}
The reason for this is that i do not want to wait for the client to send a message, all I want to do it check if a message is available else move on.
The problem is that I have no way of checking if the ServerSocket has closed.
If the ServerSocket closes, then bin.ready() is never true and it doesn't throw an exception.
One way is to see if bin.readLine() returns null but then this would involve waiting for a message which is not what I want to do.
And isConnected() dosn't tell you about the status of the ServerSocket.
Any ideas?
Thanks
Yes it's catched like this....
try
{
InputStream in = connection.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
if(bin.ready()){
input = bin.readLine();
}
catch(Exception e){input = "serverdisconnected"; System.out.println("cerror"); }
It never throws an exception hough
First...do you create every time the BufferedReader? This is not a good solution...
On this line
input = bin.readLine();
in my opinion an exception should be thrown, a SocketException I think...or an EOFException...the only thing you can do is sending every 60 seconds an ack back to the client from the server to understand if the servesocket is still alive or not
Message was edited by:
albertthe
try in this way:
if(bin.ready()){
input = bin.readLine();
}
else{
if(bin.readLine!=null)
System.out.println("Not ready")
}
The control is now unuseful, I know, but in this way you can catch the exception
> The only way to detect all possible
> server side failure scenarios from a client is for
> the client to write to the socket.
>
> There is no way to do it using a read.
>
> That is how TCP works.
You are right. I was confused. the exception is thrown when you try to write to the socket...