Solved

Set the Socket Timeout value.

Posted on 2001-09-06
9
551 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 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

23 Experts available now in Live!

Get 1:1 Help Now