1

So basically I'm writing a program which outputs a website. The website should be written directly onto the server.

I am using this code to write to the server:

URL  url = new URL("ftp://usr:[email protected]/test/login.php;type=i");
    URLConnection urlc = url.openConnection();
    OutputStream os = urlc.getOutputStream(); // To upload
    OutputStream buffer = new BufferedOutputStream(os);
    PrintStream output = new PrintStream(buffer);
    output.print(sb);

But sometimes my program runs and never terminates, so I'm guessing the connection is not being successful. '

Could I create a timeout in the case that this occurs? ie like 30 seconds, the connection would be automatically terminated so that the program could terminate.

Thanks in advance!

1

2 Answers 2

2
con.setConnectTimeout(5000);//5 seconds
con.setReadTimeout(5000);
Sign up to request clarification or add additional context in comments.

Comments

2

URLConnection has a setConnectTimeout method. By using this method you can set the time:

urlc.setConnectTimeout(30); // Time is in milliseconds
urlc.setReadTimeout(30);

2 Comments

Are you sure the parameters are not in milliseconds :) 30 is too low and throws an exception instantly. Setting to 5000ms (ie 5s), as the post below shows works perfectly though.
indeed, I thought you wrote 30 since i wrote 30 seconds in my question, so I was not sure

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.