• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a timeout for a HttpConnection

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
was only introduced to this forum a few months ago when I started a uni project using J2ME, and would just like to say that it is an excellent source of help for someone just beginning to learn to program. Hope I'm not re-treading old ground here, but I can't seem to find a decent example so here goes. All I'm trying to do is set a timeout for when my MIDlet tries to communicate with a servlet. The data I am sending/receiving is quite small, less than 2kb max, and I would like to create something which after 30 seconds (ballpark figure) of trying unsuccessfully to connect to the servlet asks the user to try again later etc. My application works fine both on the emulator and running on my phone, but I would prefer to give a user some feedback if for some reason the server is unavailable, rather than just have the application hang if it cannot connect.
Thanks to everyone in advance for your help,
Sean.
 
Author
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the web server is not listening on the port you try to connect to, or if the URL is unreachable in a network sense, an openInputStream will throw an IOException.
 
Sean McGaughey
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Daryl - didn't realise it was something so obvious - I had a catch for IOExceptions but didn't have an action to handle them- presume this was why it was just hanging. Cheers again.
Sean.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, that IOException is only thrown if there is a clear
indication from the network (i.e. Connection refused/Host unreachable)
that the server isn't listening/unreachable. If the TCP connection
attempt goes completely unanswered (e.g. due to a DROP in a firewall)
openInputStream will keep hanging until it times out. The default
timeout probably varies from platform to platform, but in the emulator
(and in a number of phones) it appears to be on the order of minutes.

I haven't yet managed to find a way to either interrupt the connecting
thread or adjust the timeout. Is this even possible??
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not start your own timer, and then wait for something back from the server? If you still have nothing when your timer is done, close the connection, sort of your own homemade timeout.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic