Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

Socket with setSoTimeout() never times out

Thread view: 
phillip.s.powell@gmail.com - 28 Feb 2008 17:45 GMT
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
Gordon Beaton - 28 Feb 2008 19:14 GMT
> I have a Socket class instance that I need to time out after a
> specific amount of time:

It isn't at all clear what you're trying to do. You create a Socket
and a BufferedReader, but immediately close the BufferedReader which
closes the Socket too (so there's no need for shutdown{In/Out}put).

Further you can't set a property on one Socket instance and expect it
to have any effect on another one used by your mailAdminReader,
regardless of what you connect to. In case that's what you're hoping.

If you can't fix this in the mailAdminReader class itself, make the
call in a separate thread, and wait at most 5 seconds for the thread
to finish.

Or if you simply want to introduce a 5s delay in your code, try
Thread.sleep().

/gordon

--
phillip.s.powell@gmail.com - 29 Feb 2008 14:22 GMT
> > I have a Socket class instance that I need to time out after a
> > specific amount of time:
>
> It isn't at all clear what you're trying to do. You create a Socket
> and a BufferedReader, but immediately close the BufferedReader which
> closes the Socket too (so there's no need for shutdown{In/Out}put).

I should have tried to make myself more clear.  MailAdminReader is a
class that will connect to a remote mail server and retrieve someone's
mail.  I am not allowed to update nor change anything in
MailAdminReader so I can do nothing about the fact that the class has
no ability to handle timeouts.  If the remote mail server is not
available, MailAdminReader method checkUserMail() will keep trying for
minutes on end ad infinitum.  What I am trying to do is to prevent
that from happening by figuring out a way to limit the time
checkUserMail() has to check remote mail while having no ability to
change MailAdminReader to do so.

Does that make more sense now?

> Further you can't set a property on one Socket instance and expect it
> to have any effect on another one used by your mailAdminReader,
[quoted text clipped - 3 lines]
> call in a separate thread, and wait at most 5 seconds for the thread
> to finish.

How would I do this within a JSP script?

> Or if you simply want to introduce a 5s delay in your code, try
> Thread.sleep().
>
> /gordon
>
> --
Gordon Beaton - 29 Feb 2008 14:58 GMT
> Does that make more sense now?

Perhaps. So you're trying to avoid invoking the MailAdminReader method
by testing first whether the machine is up?

Perhaps try something like this:

 try {
   Socket s = new Socket(); // note: unconnected
   // choose the *same* port here as mailAdminReader uses! 25, 110, 143 etc
   s.connect(new InetSocketAddress(addr, port), 5000);
   s.close();
   hasMail = mailAdminReader.checkMail();
 }
 catch (SocketTimeoutException) {
   // no response
 }

Or use a separate thread:

 public class MyRunnable implements Runnable {
   private boolean hasMail = false;
   private boolean gotResponse = false;

   public boolean gotResponse() {
     return gotResponse;
   }

   public boolean hasMail() {
     return hasMail;
   }

   public void run() {
     try {
       hasMail = mailAdminReader.checkMail();
       gotResponse = true;
     }
     catch (Exception e) {
       // no response
     }
   }
 }

Then:

 MyRunnable mr = new MyRunnable();
 Thread t = new Thread(mr);
 t.start();
 t.join(5000);
 if (mr.gotResponse()) {
   hasMail = mr.hasMail();
 }

> How would I do this within a JSP script?

Not knowing JSP, I'll hazard a guess and say the same way you'd do it
in an application.

Both of these techniques should work. The first one won't catch
situations where the mailAdminReader blocks for other reasons. The
second one will leave the thread dangling until the mailAdminReader
eventually times out, you just don't need to block waiting for that to
happen.

/gordon

--
phillip.s.powell@gmail.com - 29 Feb 2008 20:22 GMT
> > Does that make more sense now?
>
[quoted text clipped - 53 lines]
> Not knowing JSP, I'll hazard a guess and say the same way you'd do it
> in an application.

I tried to use your example, however, I wound up with multiple
compilation errors due to variables not being declared "final", which
in JSP is beyond me to know how to do to variables instantiated within
a JSP scriptlet.

> Both of these techniques should work. The first one won't catch
> situations where the mailAdminReader blocks for other reasons. The
[quoted text clipped - 5 lines]
>
> --


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.