Socket with setSoTimeout() never times out
I have a Socket class instance that I need to time out after a
specific amount of time:
<pre>
<code>
boolean hasMail = false;
Socket socket = new Socket("www.example.com", 80);
socket.setSoTimeout(5000); // IS SUPPOSED TO TIME OUT AFTER 5 SECONDS
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
in.close();
socket.shutdownOutput();
socket.shutdownInput();
hasMail = mailAdminReader.checkMail(); // CHECKS FOR MAIL ON
// SAME MAIL SERVER AS WHAT THE SOCKET WOULD BE CHECKING FOR
// CONNECTIVITY - BUT MailAdminReader CLASS HAS NO AVAILABLE
Socket PROPERTY
} catch (UnknownHostException e) {
System.out.println("Unknown host: " + e.getMessage());
} catch (InterruptedIOException e) {
System.out.println("Error attempting to connect: " +
e.getMessage());
} catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("General error: " + e.getMessage());
}
</code>
</pre>
Problem is that when I use these lines of code, the Socket instance,
instead of either connecting or timing out after 5 seconds, keeps
trying to connect after 1 - 2 minutes.
Is there something else I should be doing? All I'm trying to do is to
figure out a way to handle MailAdminReader class method
checkUserMail(), which has no way of timing out itself.
Thanks