java線上聊天項目0.8版 實現把服務端接收到的資訊返回給每一個用戶端視窗中顯示功能,線上聊天0.8

來源:互聯網
上載者:User

java線上聊天項目0.8版 實現把服務端接收到的資訊返回給每一個用戶端視窗中顯示功能,線上聊天0.8

迭代器的方式會產生鎖定

伺服器端增加發送給每個用戶端已收到資訊的功能

所以當擷取到一個socket,並開啟它的線程進行迴圈接收用戶端發來資訊時,我們把這個內部類的線程Client儲存到集合List<Client>中

然後在讀取到用戶端資訊後,把這個資訊發送給所有連接埠

通過迴圈

for(int i=0;i<clients.size();i++){

  Client c=clients.get(i);

  c.send(str);

}

發送給每一個已經成功串連到服務端的用戶端

服務端詳細的代碼修改如下:

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> clients=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("連接埠使用中......");        } 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.printStackTrace();            };        }        @Override        public void run() {            try {//注意:要包括while迴圈,如果try在while迴圈裡,則出現socket closed異常                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();//方法二,不可取,有同步鎖//                         c.send(str);//                     }                    //                    Iterator<Client> it=clients.iterator();//                    while(it.hasNext()) {//                        Client c=it.next();//方法三,不可取,有同步鎖,修改需要加鎖(此時沒修改)//                        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();                    }                }            }        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.