1

I have a problem when tryig to connect to a server using java.

Lets say that on line:

server.connect()

the program just stalls, no exceptions are thrown.

What I would like to do is throw an exception after 5 seconds if it is stalled. But since I can't get to the next line how would this be possible?

something like

if(after 5 sec server.connect is stalled){
throw an exception
 } 

And would that even solve the problem?

Sean Mc
  • 71
  • 1
  • 8

1 Answers1

0

I think you can use this:

connection.setConnectTimeout(5000);//This value is in Mili seconds
NewUser
  • 3,729
  • 10
  • 57
  • 79
  • In the API it says: void setTimeout(int timeout) sets the timeout setting. Now, this works, it throws the exception. What I'm wondering is if this is only thrown if I cannot connect. Or if it will be thrown after 5sec if it is open. Let's say I'm uploading lots of files and they take longer than the timeout? – Sean Mc Oct 10 '13 at 10:59
  • @SeanMc: Few clarifications I need, you are uploading the files or saving them to a database? – NewUser Oct 10 '13 at 11:08
  • @SeanMc: For your clarification, if you have a huge file and which cann't be uploaded in 5 seconds, then also an exception will be raised in 5 seconds. – NewUser Oct 10 '13 at 11:09
  • I'm uploading SFTP to a server to be saved in a folder – Sean Mc Oct 10 '13 at 11:11
  • @SeanMc: Then I don't have to change anything.. ;) Hope it solved your problem. – NewUser Oct 10 '13 at 11:11
  • I'm wondering if there is another way to do this because I don't want to rely on the timeout incase it timesout on my files during the upload? – Sean Mc Oct 10 '13 at 11:17