shawn
shawn

Reputation: 4223

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 );

Upvotes: 5

Views: 6405

Answers (1)

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53687

If you are using socket, you can set timeout option

Upvotes: 6

Related Questions