• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems transitioning from Thread.stop()

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I was working on a simple multithreaded chat program, and it was working fine until I decided to take heed to the deprecation errors associated with using Thread.stop(). I changed my code to Sun's reccomendations and used a boolean running, but a problem arises whenever I try to disconnect the client from the server

Here's the problematic code:


Whenever I close the client program, it always gives this error:
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at JClient.run(JClient.java:192)
at java.lang.Thread.run(Unknown Source)

and here's the run method


It appears that although the running flag is set to false, the run method is still running, so it is still listening to the input stream (in).
any suggestions would be appreciated.
-Karl Nilsson
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The running flag would terminate the loop if and when the in.readUTF() completed and you ran down to the test. But you closed the socket while in.readUTF() was waiting for input, and that caused the exception. This isn't necessarily bad. I have some code almost exactly like that. In the catch clause I check the running flag. If the flag is false I ignore the exception and exit the run method.

I think you want the message.equals() tests and logic inside the while loop, too. See what you think.
 
karl nilsson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, it works fine now
and thanks for pointing out the misplacement of the tests, after working with the run method so many times I guess I just misplaced a bracket
-Karl Nilsson
reply
    Bookmark Topic Watch Topic
  • New Topic