Set the Socket Timeout value.

Posted on 2001-09-06
Medium Priority
Last Modified: 2011-09-20

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

Any ideas?

Question by:marcoo
LVL 16

Expert Comment

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

Author Comment

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

I have the code on my PC athome and i'm at work for the moment...
LVL 16

Accepted Solution

heyhey_ earned 400 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.
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

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.

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.

Best regards,
Jim Cakalic

Expert Comment

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

Author Comment

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

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

Author Comment

ID: 6469870

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.


Expert Comment

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.
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.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

------------>  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.
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.

Expert Comment

ID: 6973186
Comment from expert accepted as answer.

E-E Moderator

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.
Suggested Courses

597 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