aspose file tools*
The moose likes Beginning Java and the fly likes Java networking, nothing happens Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java networking, nothing happens" Watch "Java networking, nothing happens" New topic
Author

Java networking, nothing happens

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Hi all.

I'm building a simple chat client and nothing happens when the client connects to the server, i think the thread is stopping on "while (message = in.readLine()) != null"



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You are accessing the same variable from two unsynchronized contexts: the go() method, and the run() method. There's no guarantee that the in and out variables will be assigned a value before the second thread starts running.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Thanks Stephan

i'm basing it on this code Head First Java in which run and setupnetworking() are two different methods sharing the variables reader and writer. Note IncomingReader() is an inner class.

Copying everything from go() into the thread run() still produces no output. I'm only running one thread.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Rob Brew wrote:i think the thread is stopping on "while (message = in.readLine()) != null"


That's quite possible. It would be waiting until it had received some data followed by some line-ending character. So if the other party is sending some data which doesn't have a line-ending character, your code would wait forever.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
You've got it Paul, nice one
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Just for clarity, I based my answer on an earlier version of the original post, where the problem was that a NullPointerException was being thrown, which implied the in variables was null, not the message.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Ah sorry Stephan, i thought i had it fixed then changed my post.
 
wood burning stoves
 
subject: Java networking, nothing happens