• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

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
 
Saloon Keeper
Posts: 9860
199
  • 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
Saloon Keeper
Posts: 9860
199
  • 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.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!