client disconnecting in a client/server pair
I have a client and server that communicate using tcp and send strings to each other using printwriter and bufferedreader.
the server has a loop for receiving messages that waits until bufferedreader.readLine() doesn't return null. On the server, calling bufferedreader.readLine() when there is no message returns null. My intention is that the readLine() will throw a socket exception on the server when the client closes the socket, ending the loop.
The client also has a loop for reading incoming messages, very similar to the one the server has. However, bufferedreader.readLine() blocks on the client until a message is received, instead of returning null.
I have two ways of closing the connection on the client's end. Either set a boolean value and have the message-reading loop stop when the boolean value changes or call socket.close() so the next readLine() throws a socket exception and breaks out of the loop. Setting the boolean value doesn't work if a call to readLine() is blocking, so I have to use socket.close(). However, if I close the socket on the client, no exception is thrown on the server. readLine() on the server just keeps returning null over and over. I tried setting a boolean value between readLine() calls on the client and this ends the loop and the client thread ends without ever having closed the socket. Interestingly enough, this does throw a socket exception on the server.
My question is if there is some way to have readLine() return null if there is no message on the client, just like on the server, or if there is a way to have the server throw a socket exception on the server when the socket is closed by the client.
If anyone wants to see the relevant code, I could post it.
[1761 byte] By [
Japhetha] at [2007-11-27 6:11:42]

# 1
> On the server, calling
> bufferedreader.readLine() when there is no message
> returns null.
No it doesn't. Read what it says in the Javadoc http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()
> My intention is that the readLine()
> will throw a socket exception on the server when the
> client closes the socket, ending the loop.
Leaving your intentions aside, what will actually happen in this case is that readLine() will return null.
> I have two ways of closing the connection on the client's end.
No, you have one way of closing the connection at either end: close the socket. The correct way to do that is to close the socket output stream.
> Either set a boolean value and have the
> message-reading loop stop when the boolean value
> changes
This might stop your loop but it doesn't close anything.
> or call socket.close() so the next readLine()
> throws a socket exception and breaks out of the loop.
No it won't.
You have a common but fundamental misunderstanding here. readLine() returns null when the other end has been closed. When there is no more data but the other end is still open, it just blocks. You can set a read timeout to stop this being forever.
ejpa at 2007-7-12 17:18:10 >

# 2
The reason I thought readLine() returns null when there is no data to read is because I was looking at what it returned on the server only after the client was closed. I was testing both on the same computer and had problems alt-tabbing when my client was fullscreen. Thanks for clearing that up.
My client had two threads and when I closed the socket and both the input stream and output stream from one thread, the other thread, the one calling readLine(), did throw a socket exception though.
# 3
>
> My client had two threads and when I closed the
> socket and both the input stream and output stream
> from one thread, the other thread, the one calling
> readLine(), did throw a socket exception though.
Yeah but you closed the socket. Or are you just agreeing?
# 4
@ EJP
If you use the undocumented url tags you can avoid the errors that come with links that end in () .
Like this:
[url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()]http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()[/url]
Produces
[url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()]http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine()[/url]