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