• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 23791
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic