aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark ""Socket is closed"... why?" Watch ""Socket is closed"... why?" New topic
Author

"Socket is closed"... why?

Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
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

Joined: Sep 17, 2006
Posts: 1282
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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

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.


[How To Ask Questions On JavaRanch]
Aji Prasetyo
Ranch Hand

Joined: Jun 13, 2007
Posts: 65
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.
 
jQuery in Action, 2nd edition
 
subject: "Socket is closed"... why?