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