jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes readLine() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "readLine()" Watch "readLine()" New topic


Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
Hi All,

My query is about readLine() and when it returns null.

I've got a chat client/server (from the good ole Head First guys) and in the server code it sets up the following runnable job that's loaded into a thread:

A message is sent to the server every time the "Send" button on the GUI (not shown) is clicked and ends up in the run() method above.

What is confounding me is that once the message has been sent, picked up in the run() method and printed out, the while loop loops back to the readLine() and as there isn't a message straight away won't it return null?

I know this isn't the case because if it were the run() method would complete and the thread would die. This doesn't happen because I can type in and send another message any time and it gets printed out so the thread must still be running.

So the question is why isn't readLine() returning null if there's nothing to read? and since it clearly isn't returnng null when will it return null

I know this must be simple but it's not clear to me and I've looked through the doco and it hasn't helped me so far.

So I'd be glad of some help.

Joanne Neal

Joined: Aug 05, 2005
Posts: 3432
An input stream read method 'blocks until input data is available, the end of the stream is detected, or an exception is thrown.'
There is therefore a difference between no data being available and the end of the stream being reached. The latter will probably not happen until the socket is closed, but this is probably dependent on what the socket is connecting to.

Phil Hopgood
Ranch Hand

Joined: Jul 14, 2008
Posts: 47
Ahh! Thanks Joanne.

I knew it had to be something like that and now you mention it I think I remember reading that somewhere

I agree. Here's the link: http://aspose.com/file-tools
subject: readLine()
Similar Threads
Client disconnected from server after about 10 minutes of inactivity
Help with Chat software
Applet + Application communication
Annotation for VerySimpleChatServer from Head First Java book
I need help improving this chat server and chat client!