Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Problem controlling a ctrl + c signal by console

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Problem controlling a ctrl + c signal by console

    hi! first of all tell me if you doesn't understand something of my post.

    I'm programming a Java chat with a server and several clients (all of them in separate consoles). Everething is alright when I run the server, then a client, the other client...

    My problem is that I have to control the "ctrl + c" signal (in the client). In the source code I've implemented this function:

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
    public void run (){
    try {
    System.out.println("Ctrl + c pressed");
    Client.disconnect(s, c);
    finalize();
    } catch (Throwable ex) {
    Logger.getLogger(Client.class.getName()).log(Level .SEVERE, null, ex);
    }
    }
    }));

    My program lets me pressed ctrl + c but it doesn't enter in this funcion because, this "Ctrl + c pressed" is never printed. That is why my server doesn't remove the client of the list...

    Does anyone know why that happend??

    Best.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem controlling a ctrl + c signal by console

    Client.disconnect(s, c);
    What are s and c?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem controlling a ctrl + c signal by console

    Ups sorry,

    ServerInterface s;
    Client c;

    They are only my instances of the server and the client. With that sentence I pretend to remove the client of the list which is managed by the server.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem controlling a ctrl + c signal by console

    Quote Originally Posted by Thabby View Post
    Ups sorry,

    ServerInterface s;
    Client c;

    They are only my instances of the server and the client. With that sentence I pretend to remove the client of the list which is managed by the server.
    Alright and where do you get the keyboard strokes against Ctrl+C?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem controlling a ctrl + c signal by console

    What do you mean? :S

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem controlling a ctrl + c signal by console

    By "Control C", do you mean sending a SIGINT to the program via the command line? I'd recommend posting an SSCCE that demonstrates the problem. For instance, from what is posted it is unclear whether the shutdown hook is ever added during the runtime of the application.

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Problem controlling a ctrl + c signal by console

    I agree with copeg - a cut down example that illustrates the problem will clear things up. (and answer questions like whether the hook ever gets added.)

    As an aside I wonder about whether a shutdown hook on the client will be enough. The slings and arrows of the web are such that the client/server link might get severed without the clean disconnect() method ever getting called. So the server might need to take some action to make sure its list of clients is valid.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem controlling a ctrl + c signal by console

    I found the problem, I used a "while(true)" loop for reading (with Scanner class) the messages that the client could send to another, and the RunTime function which cautch the signal "SIGINT". When I pressed ctrl + C (as I was a client) RunTime caught the signal but didn't give me the control line again. Now I substitute with a "do while" loop and it works. But I don't understand why. Someone can explain me why it works with the do while loop and not the other way around?

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem controlling a ctrl + c signal by console

    Quote Originally Posted by Thabby View Post
    Someone can explain me why it works with the do while loop and not the other way around?
    You have yet to give us any context to try and address your question. I presume you don't want us to guess, so please post an SSCCE

Similar Threads

  1. For Loop and String Problem - Console Hangman Game
    By ashboi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2012, 12:15 AM
  2. Unsorted Arrays having trouble controlling the array.
    By orbin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2012, 11:20 AM
  3. From Console to GUI : problem !!
    By hexwind in forum AWT / Java Swing
    Replies: 33
    Last Post: August 20th, 2011, 10:50 PM
  4. Controlling individual threads
    By youngstorm in forum Threads
    Replies: 4
    Last Post: October 19th, 2010, 03:28 PM
  5. Problem with Console Input from Clipboard
    By Nilhanth in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 1st, 2010, 06:47 PM