• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

"Socket is closed"... why?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am coding a client and using ServerSocket to receive data from server.



the problem is the first loop client can receive data from server OK. But after that when returning to the loop,

java.net.SocketException: Socket is closed



such exception message is occured.

I don't understand, because I never tell



ServerSocket to close.

Anyone knows how to put ServerSocket in loop properly?

Thank You so much beforehand.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you give us a little more code and details ?

It doesn't look that hard to me: ois.close(); {     } a Socket is just a file, from some definitions. A file is an ordered sequence of bytes. If you call close() on anything anywhere, it may propogate up the inheritance or something.

In other words, it could be that your close is closing the file.

You generally have a method accept(), that is hardcoded into the underlying machine and that has to be written on the assumption that threads will keep running into your application at that point, and you pass them off quickly - usually by calling new on something, and return as soon as possible, so that that thread that called accept() does not get crossways with other stuff or hang up or something.

There is also quite a lot of hardwiring that gets into issues like Socket.keepAlive or something .... we will have to see some more of this to avoid lengthy speculation that does not have any effect on your problem domain.
[ September 18, 2007: Message edited by: Nicholas Jordan ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this post. Since you aren't holding on to the server socket instance, it's getting garbage collected and the socket may not be available for binding.
 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply.

I got it now. Many thanks to you guys.

But the problem doesn't stop there. I've post the current problem I am facing along with my sample program in this link: https://coderanch.com/t/207488/sockets/java/synchronize-sockets

Please feel free to take a look and see if there is something you guys can help me.

Thank you for your time and concern.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic