Solved

Set the Socket Timeout value.

Posted on 2001-09-06
9
553 Views
Last Modified: 2011-09-20
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?

0
Comment
Question by:marcoo
9 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6460489
> 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
0
 

Author Comment

by:marcoo
ID: 6460624
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...
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 100 total points
ID: 6460655
hmm ... it seems that setSoTimeout() affects only socket.read() timeouts. you cannot timeout 'connect()' because Socket creates TCP connection to the remote machine inside its constructor.

all you can is start another watcher Thread and kill the socket (and connecting Thread) after some timeout.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6461466
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:ranak
ID: 6462961
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/ 
0
 

Author Comment

by:marcoo
ID: 6469864
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...
0
 

Author Comment

by:marcoo
ID: 6469870


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.

0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6878338
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.
http://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.  http://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.
http://www.experts-exchange.com/questions/Q.11323879.html
http://www.experts-exchange.com/questions/Q.11432799.html
http://www.experts-exchange.com/questions/Q.11442498.html
http://www.experts-exchange.com/questions/Q.11841938.html
http://www.experts-exchange.com/questions/Q.11888038.html
http://www.experts-exchange.com/questions/Q.11918299.html
http://www.experts-exchange.com/questions/Q.11941778.html
http://www.experts-exchange.com/questions/Q.12041699.html
http://www.experts-exchange.com/questions/Q.20000867.html
http://www.experts-exchange.com/questions/Q.20179380.html
http://www.experts-exchange.com/questions/Q.20183682.html
http://www.experts-exchange.com/questions/Q.20192185.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://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 http://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.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6973186
Comment from expert accepted as answer.

Computer101
E-E Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java passing arguments (type error) 2 48
Image decoding from Camera 3 72
java jdbc batch example 8 22
MySqlDump not dumping triggers 1 18
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now