This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

When my socket client stops, "Socket closed Exception" is thrown  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, Below codes are simple Java socket programming,

==== Server Codes =====


I think the above server application codes work well, It throws no exceptions. But the problem is that the below client socket codes throws "Socket closed Exception" when the client stops.

==== Client Codes ======


The exception is
java.net.SocketException: Socket closed
       at java.net.SocketInputStream.socketRead0(Native Method)
       at java.net.SocketInputStream.socketRead(Unknown Source)
       at java.net.SocketInputStream.read(Unknown Source)
       at java.net.SocketInputStream.read(Unknown Source)
       at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
       at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
       at sun.nio.cs.StreamDecoder.read(Unknown Source)
       at java.io.InputStreamReader.read(Unknown Source)
       at java.io.BufferedReader.fill(Unknown Source)
       at java.io.BufferedReader.readLine(Unknown Source)
       at java.io.BufferedReader.readLine(Unknown Source)
       at com.aaa.client.InputThread.run(JavaChatClient.java:66)

It seems when the "br.readLine()" line is executed, the the socket of BufferedReader objects br is already closed. So it throws Socket closed Exception.
I'd like to know how to close the BufferedReader br object automatically when the client socket connection is disconnected.
I am totally stuck here. Any advice will be appreciated. Thanks
 
Bartender
Posts: 9558
189
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are closing the socket prematurely on line 32.

Remember that your sockets are read from in separate threads, and the original thread continues with the finally clause directly after it's started the threads.

You need to wait until both the input and output threads are done before you close the socket. All of this is much easier if you use a GUI rather than System.in, because with a GUI your output can be event-driven, rather than having to use a separate thread to read from System.in. Remember though, if you want to continue with the System.in approach, do not close it afterwards, as you're doing now. System.in should never be closed.
 
Joseph Hwang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. I solved this issue by simple modification like below,



I put the input thread start line to the next of output thread line. And It works. ;)
 
Stephan van Hulst
Bartender
Posts: 9558
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a reliable fix. In theory the two threads still start at around the same time, so any difference in behavior is just because of race conditions.
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!