简体   繁体   English

如何在java Socket / SSLSocket上设置读取超时?

[英]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, 我正在使用Scanner对象从套接字读取数据并使用PrintWriter对象进行写入,

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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