The way iterators are generated locks
The server side increases the functionality that is sent to each client that has received information
So when we get to a socket and open its thread to loop the receiving client to send the message, we save the thread client of this inner class to the collection list<client>
Then, after reading the client information, send this message to all the ports
by looping
for (int i=0;i<clients.size (); i++) {
Client C=clients.get (i);
C.send (str);
}
Sent to each client that has successfully connected to the server
Detailed code changes for the server are as follows:
PackageCom.swift;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.EOFException;Importjava.io.IOException;Importjava.net.BindException;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classChatserver {Booleanstarted =false; ServerSocket SS=NULL; Socket s=NULL; List<Client> clients=NewArraylist<client>(); Public Static voidMain (string[] args) {Newchatserver (). Fun (); } Private voidFun () {Try{SS=NewServerSocket (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=NewClient (s); NewThread (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 blockE.printstacktrace (); } } } classClientImplementsRunnable {PrivateSocket S; PrivateDataInputStream Dis; PrivateDataOutputStream dos; Private BooleanConnected =false; PublicClient (Socket s) { This. S =s; Try { This. Dis =NewDataInputStream (S.getinputstream ()); This. dos =NewDataOutputStream (S.getoutputstream ()); Connected=true; } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Private voidSend (String str) {Try{dos.writeutf (str); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }; } @Override Public voidrun () {Try{//Note: to include a while loop, a socket closed exception occurs if the try is in the while loop while(connected) {String str=Dis.readutf (); System.out.println (str); for(intI=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 Two, not advisable, with synchronous lock//c.send (str);// } //iterator<client> it=clients.iterator ();//While (It.hasnext ()) {//Client C=it.next ();//method Three, not advisable, there is a synchronous lock, the modification need to lock (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 (); } } } } }}
Java Online chat Project version 0.8 enables the return of information received by the server to the display function in each client window