setting a timeout for an InputStreamreader variable
Posted
by Noona
on Stack Overflow
See other posts from Stack Overflow
or by Noona
Published on 2010-05-19T22:32:04Z
Indexed on
2010/05/19
22:40 UTC
Read the original article
Hit count: 261
I have a server running that accepts connections made through client sockets, I need to read the input from this client socket, now suppose the client opened a connection to my server without sending anything through the server's socket outputstream, in this case, while my server tried to read the input through the client socket's inputstream, an exception will be thrown, but before the exception is thrown i would like a timeout say of 5 sec, how can I do this? currently here's how my code looks like on the server side:
try
{
InputStreamReader clientInputStream = new InputStreamReader(clientSocket.getInputStream());
int c;
StringBuffer requestBuffer = new StringBuffer();
while ((c = clientInputStream.read()) != -1)
{
requestBuffer.append((char) c);
if (requestBuffer.toString().endsWith(("\r\n\r\n")))
break;
}
request = new Request(requestBuffer.toString(), clientSocket);
}
catch (Exception e) // catch any possible exception in order to keep the thread running
{
try
{
if (clientSocket != null)
clientSocket.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
System.err.println(e);
//e.printStackTrace();
}
© Stack Overflow or respective owner