• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

TIME-OUTING A SOCKET CONNECTION

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Trailboss
Posts: 24113
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PROPER NAMES ARE NOW REQUIRED!
See http://www.javaranch.com/ubb/Forum10/HTML/000180.html for details.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic