Cudmore 9 Junior Poster in Training

Hi! Here's my scenario:

I have an application that utilized the DatagramSocket class. I also have a favorite Port number that I would like it to connect to Every time I load the application. One problem with my application is that it's highly threaded, and under basic circumstances, it doesn't stop executing until all threads die, which they all eventually do automatically. I'm not going to explain the entire application, but that's the gist. My problem is that I haven't implmented mySocket.close() anywhere, because more than one thread uses the socket and I can't close it while others still need it. It's complicated, but for friendly reasons, I need to implement something that automatically closes the socket once the application decides it's safe to exit. Without doing such, oddly, and maybe because I'm running Linux, the next time my app starts, it tells me the Socket's Port is already used --- so I can tell it didn't close after the previous instance of the application had exited.

Can you give me some security pointers about how to work around such an issue.
A method that would be called before the application was about to exit would be great... I know of the applet versions, stop and destroy, but can't use them in my application. I also tried making the Socket finalize, but that option doesn't appear to be available either.

Ouf. If there's no hope, I'll have to think long and hard about how to re-structure my app. I can use a SafeShutdown method somewhere, and if an application wants to import my classes, it can call that before closing. But to run the classes as they are, I need to have my application detect when it's all done automatically. How how how how *bangs head on wall* if only I was working with GUI on this one, I could use the windowClosing event listener... But no. No GUI in this project -- GUI would be a seperate program importing my classes. If I choose to do that in the future, as an extension, then I can use SafeShutdown.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.