why the timeout never work in Java ?

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 ????
j_scriptAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
That should set the timeout for a blocking read.
What are you expecting to timeout?
0
j_scriptAuthor Commented:
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
Mick BarryJava DeveloperCommented:
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
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

j_scriptAuthor Commented:
how would I do that ?
0
Mick BarryJava DeveloperCommented:
You'd close the connection if it hadn't connected in 5 seconds.
0
Venci75Commented:
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
j_scriptAuthor Commented:
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
Mick BarryJava DeveloperCommented:
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
j_scriptAuthor Commented:
Do you have any thread code for that ?
0
Mick BarryJava DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
j_scriptAuthor Commented:
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
Mick BarryJava DeveloperCommented:
The above code can be used to test for timeout of any type of connection. Do you have your current connection code.
0
j_scriptAuthor Commented:
No, I have not. I do not know how to put them together.
0
Mick BarryJava DeveloperCommented:
Just add your connection code in the above where it says:

 // Attempt the connection

0
j_scriptAuthor Commented:
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
NetminderCommented:
Force-accepted.

Netminder
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.