• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Java networking, nothing happens

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"



 
Bartender
Posts: 15719
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28303
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got it Paul, nice one
 
Stephan van Hulst
Bartender
Posts: 15719
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah sorry Stephan, i thought i had it fixed then changed my post.
 
reply
    Bookmark Topic Watch Topic
  • New Topic