Java implements multiple client chat programs

Source: Internet
Author: User
Tags sendmsg

I reviewed Java for a day. In the evening, I wrote a heartchat 0.1 program to enable multiple clients to chat with each other.CodeAs follows:

 Import Java. Io .* ;  Import Java.net .* ;  Import Java. util .* ;  Public   Class  Heartserver {  /*  * Member variable debut  */  List <Clientthread> clients =New Arraylist <clientthread> ();  /**  * This is the portal. Check it out here ...*  @ Param  ARGs  */      Public   Static   Void  Main (string [] ARGs ){  New  Heartserver (). Start ();}  /**  * Starting the server ...* */      Public   Void  Start (){  Try  {  Boolean Iconnect = False  ; Serversocket SS = New Serversocket (1720 ); Iconnect = True  ;  While (Iconnect) {system. Out. println ( "The server port is successfully bound! " ); Socket s = SS. Accept (); clientthread currentclient = New Clientthread (s ); //  Create thread reference System. Out. println ("client found! " ); Clients. Add (currentclient );  //  Add the current client to the set                  New  Thread (currentclient). Start (); system. Out. println ( "The client process has been started! ");}}  Catch  (Ioexception e) {system. Out. println ( "Ioexception" ); E. printstacktrace ();}}  /**  * Leave a home (client process) for each client )*  */      Class Clientthread Implements  Runnable {  /*  * The member variable is coming again...  */         Private  Socket S;  Private  Datainputstream DIS;  Private  Dataoutputstream DOS;  Private  String STR;  Private   Boolean Iconnect = False  ;  /**  * Minor Structure  */ Clientthread (socket s ){  This . S = S; iconnect = True  ;}  Public   Void  Run () {system. Out. println ( "The run method is started! " );  Try  {  While  (Iconnect) {system. Out. println ( "The while loop starts in the run method and is waiting for the client to send messages ..."); DIS = New  Datainputstream (S. getinputstream (); Str = Dis. readutf (); system. Out. println (STR );  For ( Int I = 0; I <clients. Size (); I ++ ) {System. Out. println ( "Forwarding messages..." + I); clientthread C = Clients. Get (I); C. sendmsg (STR );}}}  Catch (Ioexception e) {e. printstacktrace ();}}  /**  * Forward messages. I am the master. * send messages sent to the server to each connected client.  */          Public   Void  Sendmsg (string Str ){  Try  {System. Out. println ( "Create an output pipeline! " ); DoS = New Dataoutputstream ( This . S. getoutputstream (); system. Out. println ( "Writing a message to the client! " ); Dos. writeutf (STR); system. Out. println ( "Message writing to client successful! " );}  Catch  (Ioexception e) {e. printstacktrace ();}}}} 
 Import Java. AWT .* ;  Import Java. AWT. event .* ;  Import Java. Io .* ; Import Java. Lang .* ;  Import Java.net .* ;  Public   Class Heartclient Extends  Frame {  /*  * Member method appearance...  */      Private  Textfield tftext;  Private  Textarea tacontent; Private  Socket S;  Private  Dataoutputstream DOS;  Private  Datainputstream DIS;  /**  * Note: The entry... ^ *  @ Param  ARGs  */      Public   Static   Void  Main (string [] ARGs ){ New  Heartclient (). launchframe ();}  /**  * Loading Gu  */      Public   Void  Launchframe () {tftext = New  Textfield (); tacontent = New  Textarea ();  This . Setsize (300,300 ); This . Setlocation (300,300 );  This . Tftext. addactionlistener ( New  Tflistener ());  This  . Add (tftext, borderlayout. South );  This  . Add (tacontent, borderlayout. North );  This . Addwindowlistener ( New  Windowadapter () {@ override  Public  Void  Windowclosing (invalid wevent e) {system. Exit ( 0 );}});  This  . Pack ();  This  . Connect ();  This . Setvisible ( True  );}  /**  * I am trying to connect to the server...  */      Public   Void Connect (){  Try  {S = New Socket ("127.0.0.1", 1720 ); DoS = New  Dataoutputstream (S. getoutputstream (); DIS = New  Datainputstream (S. getinputstream ());  New Thread ( New  Sendthread (). Start ();  // Dos. writeutf ("hello, I find u! "); } Catch  (Unknownhostexception e) {system. Out. println ( "Unknownhostexception" ); E. printstacktrace ();}  Catch  (Ioexception e) {system. Out. println ( "Ioexception" ); E. printstacktrace ();}  Finally  {  //  Disable Donnie... }}  /**  * Don't wait for tftext (the listener class of textfield tftext)  */      Class Tflistener Implements  Actionlistener {  Private  String STR; @ override  Public   Void  Actionreceivmed (actionevent e) {Str = Tftext. gettext (). Trim (); tftext. settext ( "");  Try  {Dos. writeutf (STR );}  Catch  (Ioexception E1) {system. Out. println ( "Ioexception" ); E1.printstacktrace ();}}}  /**  * The thread in which the client receives messages ...*  */      Class Sendthread Implements  Runnable { Private  String STR;  Private   Boolean Iconnect = False  ;  Public   Void  Run () {iconnect = True  ; Recmsg ();}  /**  * Message, look at moves, where to run... (implementation of the client to receive messages )*  @ Throws Ioexception  */          Public   Void  Recmsg (){  Try  {  While  (Iconnect) {Str = Dis. readutf (); tacontent. settext (STR );}}  Catch  (Ioexception e) {e. printstacktrace ();}}}} 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.