Link to home
Start Free TrialLog in
Avatar of marcoo
marcooFlag for Sweden

asked on

Set the Socket Timeout value.

Hi!

I'm building an application that connects to an ftp server on a specific address. But if the address doesn't exist it waits until the timeout has elapsed. I've tried to set the timeout value with the function setSoTimeout(mseconds);
But it doesn't seem to use the new value. Because it still wait's for a long time when i set it to 1500 mseconds.

I uses the Socket sock = new Socket(address, port);
method and then
sock.setSoTimeout(1500);

Any ideas?

Avatar of heyhey_
heyhey_

> But if the address doesn't exist

can you define 'doesn't exist' ?

dns name does not exist ? IP address is unreachable ?

post your code
Avatar of marcoo

ASKER

I'm testing it on my local network IpClass A where
10.0.0.1 and 10.0.0.2 is valid and. What i mean with doesn't exist is if i try to connect to for example 10.0.0.3.

I have the code on my PC athome and i'm at work for the moment...
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Cakalic
Yep -- can't set the SO_TIMEOUT until the Socket construction returns. Since constructing a client Socket object implicitly connects, there is no way (JDK 1.3-wise) to specify timeout on the connect. Note that JDK 1.4 enables manipulation of socket options before establishing or accepting connections and permits specification of timeout when establishing a connection. JDK 1.4 is currently in Beta 2.
    http://java.sun.com/j2se/

David Reilly has published a couple articles on the topic of handling network timeouts with Java sockets. He proposes several solutions involving the use of Threads and socket options as heyhey as indicated. The JavaWorld article highlights encapsulating the multithreaded network code in a single class dubbed the 'TimedSocket'. This article (the second link) details the solution, demonstrates it use in a sample application, and includes a link at the bottom where you can download the source for use in your own project.
    http://www.javacoffeebreak.com/articles/network_timeouts/
    http://www.javaworld.com/javaworld/jw-09-1999/jw-09-timeout_p.html

Best regards,
Jim Cakalic
For FTP you can use sun.net.ftp.FtpClient it is part of the jdk, though not documented.
you can decompile and get it from rt.jar
http://www.ibiblio.org/javafaq/books/secrets/secretsexamples/16/
www.sourcebot.com/sourcebot/sun/net/ftp/ 
Avatar of marcoo

ASKER

I'm testing it on my local network IpClass A where
10.0.0.1 and 10.0.0.2 is valid and. What i mean with doesn't exist is if i try to connect to for example 10.0.0.3.

I have the code on my PC athome and i'm at work for the moment...
Avatar of marcoo

ASKER



heyhey
Your idea sounds like a good one but how do you kill a thread from another thread. I tried to use the destroy method but i
got NoSuchMethodError(). And with stop nothing happend the timeout still elapsed.

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.11323879.html
https://www.experts-exchange.com/questions/Q.11432799.html
https://www.experts-exchange.com/questions/Q.11442498.html
https://www.experts-exchange.com/questions/Q.11841938.html
https://www.experts-exchange.com/questions/Q.11888038.html
https://www.experts-exchange.com/questions/Q.11918299.html
https://www.experts-exchange.com/questions/Q.11941778.html
https://www.experts-exchange.com/questions/Q.12041699.html
https://www.experts-exchange.com/questions/Q.20000867.html
https://www.experts-exchange.com/questions/Q.20179380.html
https://www.experts-exchange.com/questions/Q.20183682.html
https://www.experts-exchange.com/questions/Q.20192185.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.11848478.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
Comment from expert accepted as answer.

Computer101
E-E Moderator