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.
Claudia110799Asked:
Who is Participating?
 
vladi21Connect With a Mentor Commented:
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.
0
 
zhongbingCommented:
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.
0
 
vladi21Commented:
also look http://forum.java.sun.com/forum?13@133.CDKpacmqbtm^0@.ee78448/0

 You can catch the UNIX signals using JavaSignals on Linux platform . Go to the following page http://interstice.com/~kevinh/projects/javasignals/ 



0
All Courses

From novice to tech pro — start learning today.