• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Socket is closed"... why?

 
Aji Prasetyo
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • 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 ]
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: http://www.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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic