If you open the connection using a Socket instead of URLConnection then you can set your timeout on the Socket object.
Main Topics
Browse All TopicsI am writing an application that will connect to a site and either post or get data from that site. Now sometimes when the network is slow, either from my side or because there is too much load on the server that I am connecting to, my program hangs and keeps hanging. Thus I have to kill the process and restart it... this is really not what I want to be doing since I want this program to be automatic and able to run with the least human intervention. Below is the code that I am using
try{
in = new BufferedReader(new InputStreamReader(new URL(response_url).openStre
}catch(Exception e2){
System.out.println("Unable
}
do{
try{
line = in.readLine();
if (line != null){
delivery_line += line + "\n";
if (line.indexOf("success") != -1){
sms_sent_success = true;
line = null;
}
}
}catch (Exception e3){
System.out.println("Error reading from response site:"+e3);
line = null;
}
}while (line != null);
I have traced the problem and it happens when I try to create the bufferedReader.
Is there a way by which I could specify timeout values for the connections to the sites? In that way when the connection is not established within say nsecs then I would get an exception and I could process it.
Help please...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
objects, If I use sockets, then I need to filter out all the http header information which I get from the server. So it involves a lot of coding. Because if you use sockets, you will be dealing with tcp/ip protocol, instead of http protocol. Am I right? I guess usage of thread is a better alternatvie to this.
The Usage of Threads where the main thread spawns a child thread which does the I/O with the server and waits a specified amount of time. If the child does not finish then it kills it and spawns another child to do the task.. and this goes on.. ofcourse there will be a delay between respawns so that if there was some load on the server.. it might not be there when we reconnect.
The above was an idea that I had used before but I wanted something that was inbuilt to java.URL I will keep this question open for 5 more days. If there is no better solution than the threads one then Prasanna you will get the points.
Not sure how you'd kill the thread though (w/out using deprecated method). Cause I don't think you can interrupt a blocked IO call.
The only way I can think of to implement what you want is to write your own http protocol handler which would allow you full control over the timeout.
Sorry couldn't be more help :(
Have one thread read urls
Use a class to call the "Thread"
If the the thread is not complete after a certain time, make the Thread = null;
public class URLReader {
public static void main(String[] args){
Vector container = new Vector()
Thread cnnThread = new URLReader("http://www.cnn.
Thread foxThread = new URLReader("http://www.fox.
cnnThread.start();
foxThread.start();
container.addElement(cnnTh
container.addElement(foxTh
Timer t = new Timer(120); //seconds
boolean done = false;
while(!done){
for(int i=0;i<container.size();i++
URLReader t = (URLReader)container.eleme
if(!t.complete())
//wait
else {
if(t.getException != null)
//process
}
if(t.timeOut()) {
//break;
}
}
}
}
}
Instead of stop method, you can use join method of Thread class. Here is the sample for that.
MainClass
{
main()
{
URLReaderThread thread = new URLReaderThread(url);
thread.start();
thread.join(2000); //wait for 2 seconds.
//After 2 seconds, if the thread does not finish its work, it will be interrupted.
}
}
class URLReaderThread
{
public void run()
{
//Open the url
//Read the data from the input stream.
}
}
Instead of going for stop() method, I would prefer this ugly loop.
for(int i=0; i<count; i++)
{
if(thread.isAlive() == false) //Thread completed.
break;
Thread.sleep(100); //sleep for 100 milli seconds.
}
//I expect that by count*100 milli seconds, the thread should fetch the data from URL. else stop it.
if(thread.isAlive())
thread = null;
Have a look at the HTTPClient class, it's a HTTP implementation (similiar to URLConnection) and it supports timeouts.
http://www.innovation.ch/j
Business Accounts
Answer for Membership
by: Prasanna_HebbarPosted on 2001-06-29 at 21:39:50ID: 6240209
I am not sure whether there is a way of specifying the timeout value. But you can achieve the same result by opening the url and reading the data in a different thread and starting that thread from the main thread. If the secondary thread does not finish in a specified time period, then you can handle it in a different way.