Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java networking, nothing happens

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5590
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got it Paul, nice one
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah sorry Stephan, i thought i had it fixed then changed my post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic