Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TIME-OUTING A SOCKET CONNECTION  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
 
paul wheaton
Trailboss
Posts: 22258
Firefox Browser IntelliJ IDE Java
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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

 
Nagalinga Murthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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

 
MSD PRASAD
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!