File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes TIME-OUTING A SOCKET CONNECTION Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "TIME-OUTING A SOCKET CONNECTION " Watch "TIME-OUTING A SOCKET CONNECTION " New topic
Author

TIME-OUTING A SOCKET CONNECTION

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Hello. Using a thread i am trying to create a socket. That is
Socket mySocket = new Socket(...); At times socket creation is taking a long time. I wish to put a timer on it. Say if it takes
more than 'n' seconds, i want the thread to terminate. But the thread is blocking on the socket creation and i am finding it difficult to come out of it. For that too i had to use deprecated
stop() method from the main() method to come out.
Are there any more elegant ways of doing it?
Prasad
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 19673
    ∞

PROPER NAMES ARE NOW REQUIRED!
See http://www.javaranch.com/ubb/Forum10/HTML/000180.html for details.

permaculture forums
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by MSDPRASAD:

Hello. Using a thread i am trying to create a socket. That is
Socket mySocket = new Socket(...); At times socket creation is taking a long time. I wish to put a timer on it. Say if it takes
more than 'n' seconds, i want the thread to terminate. But the thread is blocking on the socket creation and i am finding it difficult to come out of it. For that too i had to use deprecated
stop() method from the main() method to come out.
Are there any more elegant ways of doing it?
Prasad

Nagalinga Murthy
Greenhorn

Joined: Sep 26, 2000
Posts: 2
One solution is
Class
{
public static void main(String[] a)
{
while(1){ Create and start SocketThread;}
}
SocketThread
{
boolean socketIsOk;
1.) start thread 1
2.) start thread 2
}
Thread 1
{
1.)sockIsOk = false
2.)create a new socket connection here
3.)If no exception then sockIsOk = true
}
Thread 2
{
1.)wait for 'n' seconds
2.)if sockIsOk = false then stop Thread 1
}
}
Originally posted by MSDPRASAD:

Hello. Using a thread i am trying to create a socket. That is
Socket mySocket = new Socket(...); At times socket creation is taking a long time. I wish to put a timer on it. Say if it takes
more than 'n' seconds, i want the thread to terminate. But the thread is blocking on the socket creation and i am finding it difficult to come out of it. For that too i had to use deprecated
stop() method from the main() method to come out.
Are there any more elegant ways of doing it?
Prasad

MSD PRASAD
Greenhorn

Joined: Sep 25, 2000
Posts: 2
Thanx Murthy!
I still needed to use the stop() method which is deprecated. Various options like polling the socket thread and then throwing an exception on timeout , setting socket thread as daemon thread etc. have been exhausted. Buck stops right at the new Socket();!
I am still on look-out for a soluttion.
Prasad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TIME-OUTING A SOCKET CONNECTION
 
Similar Threads
Socket is not receiving the first request after long Ideal time
Help me !! creat socket!
regarding Thread.stop()
how to set timeout to throw an exception if the server is down
Problems with threads and spawning.