944,052 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 538
  • Java RSS
Nov 7th, 2009
0

Threads in an instant messaging program

Expand Post »
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):
Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 2:42 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eleal is offline Offline
5 posts
since Nov 2009
Nov 7th, 2009
0
Re: Threads in an instant messaging program
with
java Syntax (Toggle Plain Text)
  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();
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Nov 7th, 2009
0
Re: Threads in an instant messaging program
with
java Syntax (Toggle Plain Text)
  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'?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eleal is offline Offline
5 posts
since Nov 2009
Nov 8th, 2009
0
Re: Threads in an instant messaging program
Click to Expand / Collapse  Quote originally posted by eleal ...
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.
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008
Nov 8th, 2009
0
Re: Threads in an instant messaging program
java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 8
Junior Poster
gangsta1903 is offline Offline
111 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Threads and Data Structures
Next Thread in Java Forum Timeline: How to count the number of words





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC