简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM