Yo yo, Java assembled chat room, the simplest implementation

Source: Internet
Author: User

Too yards and yards, always more feel ~ ~ ~

I've been playing for one hours.

But looking at one line of the line,

Still have a sense of accomplishment ~ ~:)

Verysimplechatserver.java

ImportJava.io.*;Importjava.net.*;ImportJava.util.*; Public classverysimplechatserver {ArrayList clientoutputstream;  Public classClientHandlerImplementsRunnable {BufferedReader reader;    Socket sock;  PublicClientHandler (Socket clientsocket) {Try{sock=Clientsocket; InputStreamReader Isreader=NewInputStreamReader (Sock.getinputstream ()); Reader=NewBufferedReader (Isreader); } Catch(Exception ex) {ex.printstacktrace (); }    }         Public voidrun () {String message; Try {             while(message = Reader.readline ())! =NULL) {System.out.println ("Read" +message);            Telleveryone (message); }        } Catch(Exception ex) {ex.printstacktrace (); }    }}     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        Newverysimplechatserver (). Go (); }     Public voidGo () {Clientoutputstream=NewArrayList (); Try{serversocket Serversock=NewServerSocket (5000);  while(true) {Socket Clientsocket=serversock.accept (); PrintWriter writer=NewPrintWriter (Clientsocket.getoutputstream ());                                Clientoutputstream.add (writer); Thread T=NewThread (NewClientHandler (Clientsocket));                T.start (); System.out.println ("Service at Port 5000 ..."); System.out.println ("Got a connection ..."); }        } Catch(Exception ex) {ex.printstacktrace (); }    }         Public voidtelleveryone (String message) {Iterator it=Clientoutputstream.iterator ();  while(It.hasnext ()) {Try{printwriter writer=(PrintWriter) it.next ();                WRITER.PRINTLN (message);            Writer.flush (); } Catch(Exception ex) {ex.printstacktrace (); }        }            }} 

Simplechatclient.java

ImportJava.io.*;Importjava.net.*;ImportJava.util.*;Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.*; Public classsimplechatclient {JTextArea incoming;    JTextField outgoing;    BufferedReader reader;    PrintWriter writer;    Socket sock;  Public Static voidMain (string[] args) {//TODO auto-generated Method StubSimplechatclient client =Newsimplechatclient ();    Client.go (); }     Public voidGo () {JFrame frame=NewJFrame ("Ludicrously simple Chat Client"); JPanel Mainpanel=NewJPanel (); Incoming=NewJTextArea (15, 30); Incoming.setlinewrap (true); Incoming.setwrapstyleword (true); Incoming.seteditable (false); JScrollPane Qscroller=NewJScrollPane (incoming);        Qscroller.setverticalscrollbarpolicy (scrollpaneconstants.vertical_scrollbar_always);        Qscroller.sethorizontalscrollbarpolicy (Scrollpaneconstants.horizontal_scrollbar_never); Outgoing=NewJTextField (20); JButton Sendbutton=NewJButton ("Send"); Sendbutton.addactionlistener (NewSendbuttonlistener ());        Mainpanel.add (Qscroller);        Mainpanel.add (outgoing);        Mainpanel.add (Sendbutton);                Setupnetworking (); Thread Readerthread=NewThread (NewIncomingreader ());                Readerthread.start ();        Frame.getcontentpane (). Add (Borderlayout.center, Mainpanel); Frame.setsize (400, 500); Frame.setvisible (true); }        Private voidsetupnetworking () {Try{sock=NewSocket ("127.0.0.1", 5000); InputStreamReader StreamReader=NewInputStreamReader (Sock.getinputstream ()); Reader=NewBufferedReader (StreamReader); Writer=NewPrintWriter (Sock.getoutputstream ()); System.out.println ("Networking established"); } Catch(IOException ex) {ex.printstacktrace (); }    }         Public classSendbuttonlistenerImplementsActionListener { Public voidactionperformed (ActionEvent ev) {Try{writer.println (Outgoing.gettext ());            Writer.flush (); } Catch(Exception ex) {ex.printstacktrace (); } outgoing.settext ("");        Outgoing.requestdefaultfocus (); }    }     Public classIncomingreaderImplementsRunnable { Public voidrun () {String message; Try {                 while(message = Reader.readline ())! =NULL) {System.out.println ("Read" +message); Incoming.append (Message+ "\ n"); }            } Catch(Exception ex) {ex.printstacktrace (); }        }    }}

Yo yo, Java assembled chat room, the simplest implementation

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.