phpgeek
phpgeek

Reputation: 11

disconnect client from server on failed write (Java)

Writing a comet like service in Java. Service only reads initial http headers then enters an indefinite loop, writing status messages every second. How can I detect client disconnection using a writing method? I'm not a Java developer, and this is just a toy project. Sorry if the answer is obvious.

Can't use nonblocking reads as http requires blocking (think this is right...)

Thanks

Upvotes: 0

Views: 282

Answers (1)

andy.xyz
andy.xyz

Reputation: 3285

The write will throw an exception when the client disconnects. Check for "java.net.SocketException: Broken pipe" in your catch block to determine that a disconnect is what caused it and tidy your resources. Any other exceptions should be logged and dealt with.

Upvotes: 1

Related Questions