Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a timeout for a HttpConnection

 
Sean McGaughey
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
Daryl Wilding-McBride
Author
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
draxie hobbs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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??
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic