| | |
Threads in an instant messaging program
![]() |
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 0
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):
This is the thread which reads 'q' from keyboard:
This is the Server (it is not complete):
Java Syntax (Toggle Plain Text)
public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; new StdinReaderThread(); try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.err.println("Could not create socket"); System.exit(-1); } while (listening){ new ServerThread(serverSocket.accept()).start(); } serverSocket.close(); } }
This is the thread which reads 'q' from keyboard:
Java Syntax (Toggle Plain Text)
public class StdinReaderThread implements Runnable{ BufferedReader stdin; public StdinReaderThread(){ Thread subpr = new Thread(this,"x"); stdin = new BufferedReader( new InputStreamReader(System.in)); subpr.start(); } public void run(){ while (true){ try{ String stdinline = stdin.readLine(); if( stdinline.startsWith("q")){ System.out.println("end"); } else if( stdinline.startsWith("m")) System.out.println("send message"); else if( stdinline.startsWith("l")) System.out.println("send login"); else if( stdinline.startsWith("u")) System.out.println("list users"); } catch (Exception e){} } } }
Last edited by eleal; 19 Days Ago at 2:42 pm.
•
•
Join Date: May 2008
Posts: 53
Reputation:
Solved Threads: 1
0
#2 18 Days Ago
with
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();
java Syntax (Toggle Plain Text)
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();
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#3 18 Days Ago
•
•
•
•
with
java Syntax (Toggle Plain Text)
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();
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'?
•
•
Join Date: May 2008
Posts: 53
Reputation:
Solved Threads: 1
0
#4 18 Days Ago
•
•
•
•
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'?
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.
•
•
Join Date: May 2008
Posts: 53
Reputation:
Solved Threads: 1
0
#5 18 Days Ago
java Syntax (Toggle Plain Text)
public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; // start the stdinReader first new StdinReaderThread().start(); try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.err.println("Could not create socket"); System.exit(-1); } while (listening){ new ServerThread(serverSocket.accept()).start(); } serverSocket.close(); } }
![]() |
Similar Threads
- Simple Client Server Instant Messaging Java code using UDP datagrams (Java)
- creating an instant messenger program in java (Java)
- Instant messaging (IT Professionals' Lounge)
- Instant Messaging System (Project) (Java)
- LAN Instant Messaging(Final Year Project) (Networking Hardware Configuration)
- Gliven, I need an idea for a program... help anyone? (Pascal and Delphi)
Other Threads in the Java Forum
- Previous Thread: Threads and Data Structures
- Next Thread: How to count the number of words
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





