How to set finalize in Ctrl-C

Please help me...
I'm working a project which is use a socket, datagram and port.
When the program is running and Ctrl-C pressed then the program is close, but the socket still open.
It's make my program can't run anymore even in Linux.
So when Ctrl-C pressed can I set my program run something to close the socket first, before the program close?
So I can run my program again.
Please help me to solve this problem.
Thanks for your help.
It's mean so much for me.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

but why you use ctrl-c to stop your program? You can use a menu, or read STDIN, to get the quit command from user.
23. (Sect. 8) How can I trap Control-C in Java?

     [*] Control-C is used on some OS's to break into a running program
     interactively and terminate it. On Unix Control-C is sent to the
     process as a signal. If a C program declares a handler for that signal,
     the program will be able to continue even if Control-C is sent to it.
     Control-C is not a Java concept, and there is no way to do this in pure
     Java. You can write the signal handler in C however, and impose the
     handler by calling the C routine through the Java Native Interface.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
also look^0@.ee78448/0

 You can catch the UNIX signals using JavaSignals on Linux platform . Go to the following page 

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.