Java online chat project version 0.8 provides the function of returning the information received by the server to each client window.
The iterator will cause locking.
The server adds the function of sending messages to each client that has received the information.
Therefore, when a socket is obtained and Its thread is opened to receive information from the Client cyclically, we save the thread Client of this internal class to the collection List <Client>.
After reading the client information, send this information to all ports.
Loop
For (int I = 0; I <clients. size (); I ++ ){
Client c = clients. get (I );
C. send (str );
}
Send to each client that has been successfully connected to the server
The server code is modified as follows:
Package com. swift; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. EOFException; import java. io. IOException; import java.net. bindException; import java.net. serverSocket; import java.net. socket; import java. util. arrayList; import java. util. iterator; import java. util. list; public class ChatServer {boolean started = false; ServerSocket ss = null; Socket s = null; List <Client> cli Ents = new ArrayList <Client> (); public static void main (String [] args) {new ChatServer (). fun ();} private void fun () {try {ss = new ServerSocket (8888); started = true;} catch (BindException e) {System. out. println ("port in use ...... ");} catch (IOException e1) {e1.printStackTrace ();} try {while (started) {s = ss. accept (); System. out. println ("a client connected success"); Client c = new Client (s ); New Thread (c ). start (); clients. add (c) ;}} catch (EOFException e) {System. out. println ("client has closed. ");} catch (Exception e) {e. printStackTrace ();} finally {try {ss. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} class Client implements Runnable {private Socket s; private DataInputStream dis; private DataOutputStream dos; private boolean Connected = false; public Client (Socket s) {this. s = s; try {this. dis = new DataInputStream (s. getInputStream (); this. dos = new DataOutputStream (s. getOutputStream (); connected = true;} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} private void send (String str) {try {dos. writeUTF (str);} catch (IOException e) {// TODO Auto-generated catch block e. printS TackTrace () ;}}@ Override public void run () {try {// Note: to include a while LOOP, if try is in a while loop, the socket closed exception occurs while (connected) {String str = dis. readUTF (); System. out. println (str); for (int I = 0; I <clients. size (); I ++) {Client c = clients. get (I); c. send (str);} // for (Iterator <Client> it = clients. iterator (); it. hasNext ();) {// Client c = it. next (); // method 2, not desirable, with a synchronization lock // c. send (str); // Iterator <Client> it = clients. Iterator (); // while (it. hasNext () {// Client c = it. next (); // method 3, which is not desirable and has a synchronization lock. You need to lock the modification (not modified at this time) // c. send (str); // }}} catch (IOException e) {e. printStackTrace ();} finally {if (dis! = Null) {try {dis. close () ;}catch (IOException e) {e. printStackTrace () ;}} if (s! = Null) {try {s. close () ;}catch (IOException e) {e. printStackTrace () ;}} if (dos! = Null) {try {dos. close ();} catch (IOException e) {e. printStackTrace ();}}}}}}