Yesterday I recalled a real-time group chat applet. Written in JAVA. The development procedure is as follows: this is process-oriented development. Step 1: Create a server. Step 2: Create a client and connect to the server. Step 3: The client sends the message to the server and receives the message. Step 4: implementation... yesterday I recalled a real-time group chat applet. Written in JAVA. The development procedure is as follows:
This is process-oriented development.
Step 1: Create a server.
Step 2: Create a client and connect to the server.
Step 3: The client sends the message to the server and receives the message.
Step 4: connect multiple clients to the server and receive messages from multiple clients. Multi-thread and asynchronous methods can be used to solve the situation where the server is occupied.
Step 5: The server forwards the information sent by the client to each client.
Step 6: eliminate bugs.
ChatServer. java
Import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. EOFException; import java. util. *; import java. io. IOException; import java.net. bindException; import java.net. serverSocket; import java.net. socket; import java.net. socketException; public class ChatServer {boolean started = false; ServerSocket ss = null; List
Clients = new ArrayList
(); Public static void main (String [] args) {new ChatServer (). start () ;}public void start () {try {ss = new ServerSocket (8881);} catch (BindException e) {System. out. println ("The port is in use! "); System. out. println (" Please end the process! "); System. exit (0);} catch (Exception e) {e. printStackTrace ();} try {started = true; while (started) {Socket s = ss. accept (); // obstructive function, constantly receiving Client connection c = new Client (s); // no new dynamic method in static main, therefore, the startup process is encapsulated into a public function System. out. println ("a client connected"); new Thread (c ). start (); clients. add (c) ;}} catch (Exception e) {e. printStackTrace ();} finally {try {ss. close ();} catch (IOException e ){/ /TODO Auto-generated catch block e. printStackTrace () ;}} class Client implements Runnable {// distinguishes between clients. asynchronous methods can also be used to operate a Client using a thread. Here we use threads to compare resources. Wait until I write the asynchronous method before sharing it. Private Socket s = null; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean bConnected = false; public Client (Socket s) {this. s = s; try {dis = new DataInputStream (s. getInputStream (); dos = new DataOutputStream (s. getOutputStream (); bConnected = true;} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public void send (String str ){/ /Try {dos. writeUTF (str);} catch (IOException e) {// TODO Auto-generated catch block clients. remove (this); System. out. println ("a customer quit! List removed "); // e. printStackTrace () ;}} public void run () {try {while (bConnected) {String str; str = dis. readUTF (); // obstructive function, which will always occupy the thread resource System. out. println (str); for (int I = 0; I <clients. size (); I ++) {Client c = clients. get (I); c. send (str) ;}} catch (EOFException e) {System. out. println ("a customer has exited! ");} Catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} finally {if (dis! = Null) try {dis. close (); if (dos! = Null) dos. close (); if (s! = Null) s. close ();} catch (SocketException e) {clients. remove (this); System. out. println ("a customer has exited! ");} Catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}}
ChatClient. java
Import java. awt. borderLayout; import java. awt. frame; import java. awt. textArea; import java. awt. textField; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. EOFException; import java. io. IOException; import java.net. socket; import java.net. S OcketException; import java.net. unknownHostException; public class ChatClient extends Frame {Socket s = null; DataOutputStream dos = null; DataInputStream dis = null; private boolean bConnected = false; TextField tfTxt = new TextField (); // input box TextArea taContent = new TextArea (); // display the chat information box public static void main (String [] args) {new ChatClient (). lanunchFrame ();} public void lanunchFrame () {setlow.o N (400,300); this. setSize (300,300); add (tfTxt, BorderLayout. SOUTH); add (taContent, BorderLayout. NORTH); pack (); // Adjust the gap this. addWindowListener (new WindowAdapter () {@ Override public void windowClosing (expose wevent e) {disconnect (); System. exit (0) ;}}); tfTxt. addActionListener (new TexFileListener (); setVisible (true); connect (); new Thread (new RecvThread ()). start ();} public void connect () {try {s = ne W Socket ("127.0.0.1", 8881); dos = new DataOutputStream (s. getOutputStream (); dis = new DataInputStream (s. getInputStream (); System. out. println ("connected! "); BConnected = true;} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} public void disconnect () {try {dos. close (); dis. close (); s. close () ;} catch (Exception e1) {e1.printStackTrace () ;}/ * try {bConnected = false; // ensure recvThread. join ();} catch (InterruptedException e) {e. printStackTrace ();} finally {try {dos. close (); dis. close (); s. c Lose ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} */} private class TexFileListener implements ActionListener {public void actionreceivmed (ActionEvent e) {String str = tfTxt. getText (). trim (); // taContent. setText (str); tfTxt. setText (""); try {dos. writeUTF (str); dos. flush (); // dos. close () ;}catch (IOException e1) {e1.printStackTrace () ;}} private clas S RecvThread implements Runnable {public void run () {try {while (bConnected) {String str = dis. readUTF (); // System. out. println (str); taContent. setText (taContent. getText () + str + '\ n') ;}} catch (SocketException e) {System. out. println ("exited! ");} Catch (EOFException e) {System. out. println (" exited! ");} Catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}
For more articles related to the real-time group chat mini-program development record, please follow the PHP Chinese network!