• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic