Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems transitioning from Thread.stop()  RSS feed

 
karl nilsson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!