• 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

Closing/Opening again a Socket

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

My project work is getting a bit deeper now.

What I'm trying to achieve is to be able to close the socket with a button, and be able to open the socket again.

I've got 2 buttons, one is labeled OK and another one, labeled STOP.
I want to be able to press the STOP button to close the socket, then be able to restart it with the OK button.

I'm having no luck so far.
I'm having difficulty achieving this. For now, the STOP button won't stop it.

So, here's the relevant code:









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

This is what I did, and so far seems to work. however, I seem to bump into another snag.

When I press the STOP button, it stops. here's the snag:
1. When the STOP button is pressed, the ServerThread thorws a SocketException. What I don't quite understand yet is why the ServerThread would still attempt to run when the socket.close() is called.










[ July 17, 2005: Message edited by: Karen Baog ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karen,

The reason you're getting a SocketException is because you're attempting to close a socket that is currently blocking on I/O. What may help you is to change the


To specify a break condition, i.e.



In this case, your action method could send a message to your ServerThread object to set this boolean flag. Once you break out of the loop, you should be able to safely (deterministically) close the socket. That is to say, when you're no longer blocking on I/O.

Good luck.

[ July 17, 2005: Message edited by: Jon Cone ]
[ July 17, 2005: Message edited by: Jon Cone ]
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As suggested, but it still throws the SocketException. Nevertheless, printing out the exception says "socket closed". I wonder if I just ignore this.




[ July 17, 2005: Message edited by: Karen Baog ]
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I sorted it out.

Thanks, Jon, for your reply. It helped a great deal.
 
Jonathan Cone
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to close the socket from inside the same thread as the one that it originated from. There are a couple of reasons for this:

1) Encapsulation: Good OO Design dictates that object should "take care of their own business". Remember your action method (and associated object) has nothing to do with the act of closing the socket -- its just the means to the end. This job belongs to the object (running thread) itself.

2) In your case, this is causing an undesired effect. You are putting in a stop request to the socket managing thread, but you are trying to stop the thread from another thread which is has entered the running state before the request to stop the socket has been completed (i.e. it has not left the blocked state). Remember that the order of thread execution is not guaranteed. A crude example: imagine you're standing in a door way and I ask you to get on one side or the other so I can close the door, however, before you begin to even move, I shut the door with you in the middle.

What I'm saying is handle closing the socket after the while loop stops executing.
reply
    Bookmark Topic Watch Topic
  • New Topic