Java Chat Room Simple version

Source: Internet
Author: User

Single Thread.

Server Side

1 Importjava.io.IOException;2 Importjava.net.*;3 ImportJava.io.*;4  Public classChatserver {5 6     7     8      Public Static voidMain (string[] args) {9         BooleanStarted=false;TenServerSocket ss=NULL;//Initialize OneSocket s=NULL; ADataInputStream dis=NULL; -         Try { -ss=NewServerSocket (8888);//Port number 8888,tcp the}Catch(bindexception e) { -System.out.println ("Port in use"); -System.exit (0); -}Catch(IOException e) { +E.printstacktrace ();//gives the method's calling program, until the exception is generated. -         } +         Try{ AStarted=true;//Connect on at              while(started) -             { -                 BooleanBconnected=false; -s=ss.accept (); -SYSTEM.OUT.PRINTLN ("A client connected!"); -Bconnected=true;//becomes true after receiving the other party indis=NewDataInputStream (S.getinputstream ()); -                  while(bconnected) {//there's something to attend. toString Str=dis.readutf ();//Block Type + System.out.println (str); -                 } the                 //dis.close (); *             } $}Catch(eofexception e) {Panax NotoginsengSystem.out.println ("Client closed!"); -         }  the         Catch(IOException e) { + e.printstacktrace (); A}finally{ the             Try{ +                 if(Dis! =NULL) Dis.close (); -                 if(s! =NULL) S.close (); $}Catch(IOException E1) { $ e1.printstacktrace (); -             } -         } the  -     }Wuyi  the}

Client

1 Importjava.awt.*;2 Importjava.awt.event.*;3 Importjava.io.IOException;4 Importjava.net.*;5 ImportJava.io.*;6  Public classChatClientextendsframe{7     8Socket s=NULL;9DataOutputStream dos=NULL;Ten      OneTextField tftxt=NewTextField ();//only one line can be written, there is an action ATextArea tacontent=NewTextArea ();//tags define multiple lines of text input controls -      -  the      Public Static voidMain (string[] args) { -         Newchatclient ().   Launchframe ();  -     } -      +      Public voidLaunchframe () -     { +SetLocation (400,300); A          This. SetSize (300,300); at Add (tftxt,borderlayout.south); - Add (Tacontent,borderlayout.north); - pack (); -          This. Addwindowlistener (NewWindowadapter () {//Close Window -  - @Override in              Public voidwindowclosing (windowevent e) { - disconnect (); toSystem.exit (0); +             } -              the});//Anonymous Class *Tftxt.addactionlistener (NewTflistener ()); $SetVisible (true);Panax Notoginseng Connect (); -     } the      +      Public voidConnect () A     { the         Try { +s=NewSocket ("127.0.0.1", 8888); -dos=NewDataOutputStream (S.getoutputstream ()); $System.out.println ("connected!"); $}Catch(unknownhostexception e) { - e.printstacktrace (); -}Catch(IOException e) { the e.printstacktrace (); -         }Wuyi          the     } -      Wu      Public voidDisconnect ()//Close Method -     { About         Try{ $ dos.close (); - S.close ();  -}Catch(IOException e) { - e.printstacktrace (); A         } +          the  -     } $     Private classTflistenerImplementsActionListener the     { the          Public voidactionperformed (ActionEvent e) {//One hit Enter theString str=tftxt.gettext (). Trim (); the Tacontent.settext (str); -Tftxt.settext ("");//empty after carriage return in             Try { the                 //dataoutputstream dos=new DataOutputStream (S.getoutputstream ()); theDos.writeutf (str);//output the stream. About Dos.flush (); the                 //dos.close (); the}Catch(IOException E1) { the e1.printstacktrace (); +             } -              the             Bayi         } the          the}//Inner class -}

Java Chat Room Simple version

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.