简体   繁体   中英

How do I set read timeouts on java Socket/SSLSocket?

If I have a connected socket and and it is waiting(blocking) to receive some data from the remote machine but somehow or other, the remote machine loses its connection, how can I make it such that it stops blocking but times out after a certain period of time without receiving any data? I'm using a Scanner object to read data from the socket and a PrintWriter object to write,

// Get InputStream
scanner = new Scanner( clientSocket.getInputStream() );
// Get OutputStream
printWriter = new PrintWriter( clientSocket.getOutputStream(), true );

如果使用套接字,则可以设置超时选项

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