Solved

why the timeout never work in Java ?

Posted on 2002-05-18
16
180 Views
Last Modified: 2010-03-31
here is my code :

....
Socket ws = new Socket("192.168.3.1", 1433);
ws.setSoTimeout(5000);
.....

I believe I have set the timeout valur properly.
But why it does work ?

How to make it ready timeout in 5 seconds ????
0
Comment
Question by:j_script
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7019024
That should set the timeout for a blocking read.
What are you expecting to timeout?
0
 

Author Comment

by:j_script
ID: 7019050
I see. I want to detect whether a SQL Server is alive and
can be connected before doing jdbc.

And if someone shutdown the SQL server, then above class will
take time to know that SQL Server cannot be connected.

Do you have better idea ?

I want to know whether the SQL Server is alive within 8 seconds.
0
 
LVL 92

Expert Comment

by:objects
ID: 7019069
AFAIK Java does not support this (though check 1.4, not sure if it's been added).
The only way I can think of would be to use a seperate thread to handle the timeout.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:j_script
ID: 7019423
how would I do that ?
0
 
LVL 92

Expert Comment

by:objects
ID: 7020534
You'd close the connection if it hadn't connected in 5 seconds.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7021255
as objects said - this is a wrong way to do this.
If you want to check whether the connection is still alive - you can send some data representing 'is alive?' network packages each 5 seconds. When there is an error in this communication - either the 'is alive?' data cannot be sent or the response of this package (which means 'yes - I am alive!') cannot be received - then the connection is broken.

... but I think that in this case the objects's suggestion is the better approach.
0
 

Author Comment

by:j_script
ID: 7021372
objects,

So, I am think two threads. One thread use socket connect
to the server, and other thread will check a variable.
If that variable is null after 5 seconds, means the first
thread still try to connect to the server, then second
thread will kill first thread, right ?
0
 
LVL 92

Expert Comment

by:objects
ID: 7022718
Yes that's correct.
Though you may not be able to stop the thread making the connection because it is blocked on IO. If this is the case then you'll just need to inform whoever is waiting on the connection, that connection failed.
0
 

Author Comment

by:j_script
ID: 7082895
Do you have any thread code for that ?
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7082994
Try something like:

void connect()
{
   worker.start();

   syncronized(result)
   {
      try
      {
         // Wait until connection is successful,
         // or wait times out

         result.wait(timeout);
      }
      catch (Exception e)
      {
         System.out.println(e);
      }
   }
}

public void run()
{
   // Attempt the connection

   syncronized(result)
   {
      try
      {
         // notify the connect() method

         result.notify();
      }
      catch (Exception e)
      {
         System.out.println(e);
      }
   }
}
0
 

Author Comment

by:j_script
ID: 7092398
Objects,

I will increase another 100 points for you.
Can you modify you code, so it is really only testing for
a connection to MS SQL Server ?
0
 
LVL 92

Expert Comment

by:objects
ID: 7094128
The above code can be used to test for timeout of any type of connection. Do you have your current connection code.
0
 

Author Comment

by:j_script
ID: 7094777
No, I have not. I do not know how to put them together.
0
 
LVL 92

Expert Comment

by:objects
ID: 7094789
Just add your connection code in the above where it says:

 // Attempt the connection

0
 

Author Comment

by:j_script
ID: 7094837
objects,

I am very new to Java Thread. I have modified the code.
Please take a look :

public class TEST extends Thread{
  public static void main(String[] argc){
    TestConnect tc = new TestConnect();
    tc.start();
    syncronized(result){
      try{
        result.wait(8000);
      }
      catch(Exception e){
        System.out.println(e);
      }
    }
  }
}

class TestConnect extends Thread{
  public void run(){
    // My MS SQL Server Connection code here
    syncronized(result){
      try{
        result.notify();
      }
      catch(Exception e){
        System.out.println(e);
      }
    }
  }
}

I do not know if my modification is correct.
I want to wait for 8 seconds. If after 8 seconds, there is
still no connection, how do I kill the thread or maybe
print out whether the connection is good/bad ?
0
 
LVL 5

Expert Comment

by:Netminder
ID: 8581100
Force-accepted.

Netminder
EE Admin
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 82
ArrayIndexOutOfBoundException 9 80
eclipse java  build path 6 56
Error trying to install RTMT Win7 5 59
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…
The viewer will learn how to implement Singleton Design Pattern in Java.

785 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