Threads in an instant messaging program

Reply

Join Date: Nov 2009
Posts: 5
Reputation: eleal is an unknown quantity at this point 
Solved Threads: 0
eleal eleal is offline Offline
Newbie Poster

Threads in an instant messaging program

 
0
  #1
22 Days Ago
I'm trying to write an instant messaging program in Java. What I have coded so far, goes like this: Each client connects to a thread created by the server, to which he/she sends the message. My doubt is, how can I kill all the threads that have been created by the main program when the administrator presses de 'q' in the keyboard. I made a class for a thread whose only function is to read the 'q' from the keyboard, but, how can I notify to the main program that it must kill all the threads?

This is the Server (it is not complete):
  1. public class Server {
  2. public static void main(String[] args) throws IOException {
  3. ServerSocket serverSocket = null;
  4. boolean listening = true;
  5. new StdinReaderThread();
  6.  
  7. try {
  8. serverSocket = new ServerSocket(4444);
  9. } catch (IOException e) {
  10. System.err.println("Could not create socket");
  11. System.exit(-1);
  12. }
  13.  
  14. while (listening){
  15. new ServerThread(serverSocket.accept()).start();
  16. }
  17. serverSocket.close();
  18. }
  19. }

This is the thread which reads 'q' from keyboard:

  1. public class StdinReaderThread implements Runnable{
  2. BufferedReader stdin;
  3.  
  4. public StdinReaderThread(){
  5. Thread subpr = new Thread(this,"x");
  6. stdin = new BufferedReader( new InputStreamReader(System.in));
  7. subpr.start();
  8. }
  9. public void run(){
  10. while (true){
  11. try{
  12. String stdinline = stdin.readLine();
  13. if( stdinline.startsWith("q")){
  14. System.out.println("end");
  15. }
  16. else if( stdinline.startsWith("m"))
  17. System.out.println("send message");
  18. else if( stdinline.startsWith("l"))
  19. System.out.println("send login");
  20. else if( stdinline.startsWith("u"))
  21. System.out.println("list users");
  22. } catch (Exception e){}
  23. }
  24. }
  25. }
Last edited by eleal; 22 Days Ago at 2:42 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training
 
0
  #2
22 Days Ago
with
  1. join();

if you keep all the threads you created in an array,vector...
then you can call join() on all threads to kill them all...Of course, this will cause all threads to be done.

If you mean stopping server imediately, then just close the server...
server.close();
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: eleal is an unknown quantity at this point 
Solved Threads: 0
eleal eleal is offline Offline
Newbie Poster
 
0
  #3
22 Days Ago
Originally Posted by gangsta1903 View Post
with
  1. join();

if you keep all the threads you created in an array,vector...
then you can call join() on all threads to kill them all...Of course, this will cause all threads to be done.

If you mean stopping server imediately, then just close the server...
server.close();
Thanks gangsta1903,
But how can I notify the main program that it's child thread StdInReaderThread has just detected a 'q' from the keyboard ? And, what happens if the main program is blocked on a socket and StdInReaderThread detects a 'q'?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training
 
0
  #4
22 Days Ago
Originally Posted by eleal View Post
Thanks gangsta1903,
But how can I notify the main program that it's child thread StdInReaderThread has just detected a 'q' from the keyboard ? And, what happens if the main program is blocked on a socket and StdInReaderThread detects a 'q'?
Your approach is right. On server side, before starting the server-client mechanism, first start your stdInReaderThread. Its up to you what to do when you get "q". You can just close the server. But,for example if its a file sharing program, then you should probably wait last file transfer before closing. But in your program its just messaging, so just closing the server will serve the purpose...
What I mean, there wont be a blocking... Client sends message and it comes instantly, its not a long lasting process. Of course when you close the server, messaging if any will be interrupted.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 54
Reputation: gangsta1903 is an unknown quantity at this point 
Solved Threads: 1
gangsta1903 gangsta1903 is offline Offline
Junior Poster in Training
 
0
  #5
22 Days Ago
  1. public class Server {
  2. public static void main(String[] args) throws IOException {
  3. ServerSocket serverSocket = null;
  4. boolean listening = true;
  5. // start the stdinReader first
  6. new StdinReaderThread().start();
  7.  
  8. try {
  9. serverSocket = new ServerSocket(4444);
  10. } catch (IOException e) {
  11. System.err.println("Could not create socket");
  12. System.exit(-1);
  13. }
  14.  
  15. while (listening){
  16. new ServerThread(serverSocket.accept()).start();
  17. }
  18. serverSocket.close();
  19. }
  20. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC