Java online chat project version 0.8 provides the function of returning the information received by the server to each client window.

Source: Internet
Author: User

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 ();}}}}}}

 

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.