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

[882 byte] By [Blanchy123a] at [2007-11-27 2:30:39]
# 1
try and connect to it. if you can't, it's closed
georgemca at 2007-7-12 2:44:37 > top of Java-index,Java Essentials,Java Programming...
# 2
What I mean is if I am already connected to the ServerSocket, and then the ServerSocket closes down for whatever reason.
Blanchy123a at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 3
see reply #1
georgemca at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 4
Surely there's a better way than connecting to the server 60 times a second. :/
Blanchy123a at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 5
So, you want a method on the client to understand if the serversocket has closed?An exception will be surely thrown if the ServerSocket is closed....catch it and block your codeMessage was edited by: albertthe
albertthea at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 6

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

Blanchy123a at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 7

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

albertthea at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 8
input = bin.readLine(); does throw an exception if the server is closed down, but my code never reaches there because bin.ready() will always be false at that stage.
Blanchy123a at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 9
input = bin.readLine();make a control on input, or move the line also before the control. you have to be able to catch that exception....this is the standard way to signal the down of the connection
albertthea at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 10

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

albertthea at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 11
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.
jschella at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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...

albertthea at 2007-7-12 2:44:38 > top of Java-index,Java Essentials,Java Programming...