java網(wǎng)絡(luò)編程之群聊功能
本文實例為大家分享了java網(wǎng)絡(luò)編程之群聊功能的具體代碼,供大家參考,具體內(nèi)容如下
1、服務(wù)端
package networkCoding; ? import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.CopyOnWriteArrayList; ? /** ?*? ?* 1, 指定端口,使用serverSocket創(chuàng)建服務(wù)器 ?* 2, 阻塞式等待連接accept ?* 3, 操作:輸入輸出流操作 ?* 4, 釋放資源 ?*? ?* 5,加入容器實現(xiàn)群聊 ?*? ?* **/ ? public class WeiHuShanChatRoomServer { ? ? private static CopyOnWriteArrayList<Chat> all= new CopyOnWriteArrayList<Chat>(); ? ?? ?public static void main(String[] args) throws IOException { ?? ??? ?System.out.println("-----server"); ?? ??? ? // 1, 指定端口,使用serverSocket創(chuàng)建服務(wù)器 ?? ??? ?ServerSocket server= new ServerSocket(9999); ?? ??? ? // 2, 阻塞式等待連接accept ?? ??? ?while(true) { ?? ??? ?Socket client=server.accept(); ?? ??? ?Chat chat= new Chat(client); ?? ??? ?// 交給容器管理 ?? ??? ?all.add(chat); ?? ??? ?new Thread(chat) .start(); ?? ??? ?} ?? ?} ?? ?static class Chat implements Runnable{ ?? ??? ?private DataOutputStream dos; ?? ??? ?private DataInputStream dis; ?? ??? ?private Socket client; ?? ??? ?private boolean isRuning; ?? ??? ?private String name; ?? ??? ?public Chat(Socket client) { ?? ??? ??? ?this.client=client; ?? ??? ??? ?this.isRuning=true; ?? ??? ??? ?try { ?? ??? ??? ??? ?this.dis = new DataInputStream(client.getInputStream()); ?? ??? ??? ??? ?this.dos=new DataOutputStream(client.getOutputStream()); ?? ??? ??? ??? ?this.name=receive(); ?? ??? ??? ??? ?this.send(this.name+",威虎山歡迎你的到來"); ?? ??? ??? ??? ?this.sendOthers(this.name+"來到了威虎山",true); ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// 出錯釋放資源 ?? ??? ??? ??? ?System.out.println("===1=="); ?? ??? ??? ??? ?this.release(); ?? ??? ??? ? ?? ??? ??? ?} ?? ??? ?} ?? ??? ?private String receive() { ?? ??? ??? ?String data=""; ?? ??? ??? ?try { ?? ??? ??? ??? ?data= dis.readUTF(); ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?System.out.println("===2=="); ?? ??? ??? ??? ?//this.release(); ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ?return data; ?? ??? ?} ?? ??? ?// 群聊 ?? ??? ?private void send(String data) { ?? ??? ??? ?try { ?? ??? ??? ??? ?dos.writeUTF(data); ?? ??? ??? ??? ?dos.flush(); ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?System.out.println("===3=="); ?? ??? ??? ??? ?this.release(); ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ?} ?? ??? ?private void sendOthers(String data,boolean isSys) { ?? ??? ??? ?boolean isPrivate = data.startsWith("@");? ?? ??? ??? ?if(isPrivate) { ?? ??? ??? ??? ?int index= data.indexOf(":"); ?? ??? ??? ??? ?String targetName=data.substring(1,index); ?? ??? ??? ??? ?String msg=data.substring(index+1); ?? ??? ??? ??? ?for (Chat chat : all) { ?? ??? ??? ??? ??? ?if(chat.name.equals(targetName)) { ?? ??? ??? ??? ??? ??? ?System.out.println("******"+chat.name+targetName); ?? ??? ??? ??? ??? ??? ?chat.send(this.name+"悄悄對你說:"+msg); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ?}else { ?? ??? ??? ??? ?for (Chat chat : all) { ?? ??? ??? ??? ??? ?if(chat==this) { ?? ??? ??? ??? ??? ??? ?continue; ?? ??? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ??? ?if(isSys) { ?? ??? ??? ??? ??? ??? ??? ?chat.send(data); ?? ??? ??? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ??? ??? ?chat.send(this.name+"對大家說:"+data); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ??? ?private void release() { ?? ??? ??? ?this.isRuning=false; ?? ??? ??? ?Utils.close(dis,dos,client); ?? ??? ??? ?all.remove(this); ?? ??? ??? ?sendOthers(this.name+"離開了威虎山", true); ?? ??? ?} ?? ??? ?@Override ?? ??? ?public void run() { ?? ??? ??? ?while(isRuning) { ?? ??? ??? ??? ?String data = receive(); ?? ??? ??? ??? ?if(!data.equals("")) { ?? ??? ??? ??? ??? ? sendOthers(data,false); ?? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ?send("不能發(fā)送空消息"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ?} ? }
2、客戶端
package networkCoding; ? import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; ? /** ?*? ?* 1, 建立連接,使用socket 創(chuàng)建客戶端 + 服務(wù)端的地址端口號; ?* 2, 操作:輸入輸出流操作 ?* 3, 釋放資源 ?*? ?* **/ ? public class WeiHuShanChatRoomClient { ? ?? ?public static void main(String[] args) throws UnknownHostException, IOException { ?? ??? ?BufferedReader bf= new BufferedReader(new InputStreamReader(System.in)); ?? ??? ?System.out.println("請輸入姓名"); ?? ??? ?String bfString = bf.readLine(); ?? ??? ?//1, 建立連接,使用socket 創(chuàng)建客戶端 + 服務(wù)端的地址端口號; ?? ??? ?Socket client = new Socket("localhost",9999); ?? ??? ?// 2, 操作:輸入輸出流操作 ?? ??? ?new Thread(new Send(client,bfString)).start(); ?? ??? ?new Thread(new Receive(client)).start(); ?? ??? ? ?? ?} ?? ? ? }
(1)發(fā)送封裝類
package networkCoding; ? import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; ? public class Send implements ?Runnable{ ?? ?private BufferedReader bf; ?? ?private DataOutputStream dos; ?? ?private Socket client; ?? ?private boolean isRuning; ?? ?private String name; ?? ?public Send(Socket client,String name) { ?? ??? ?this.client=client; ?? ??? ?this.isRuning=true; ?? ??? ?this.name=name; ?? ??? ?this.bf= new BufferedReader(new InputStreamReader(System.in)); ?? ??? ?try { ?? ??? ??? ?this.dos=new DataOutputStream(client.getOutputStream()); ?? ??? ??? ?this.send(name); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// 出錯釋放資源 ?? ??? ??? ?System.out.println("===4=="); ?? ??? ??? ?this.release(); ?? ??? ??? ?this.isRuning=false; ?? ??? ?} ?? ?} ?? ?private void release() { ?? ??? ?this.isRuning=false; ?? ??? ?Utils.close(dos,client); ?? ?} ?? ?private void send(String data) { ?? ??? ?try { ?? ??? ??? ?dos.writeUTF(data); ?? ??? ??? ?dos.flush(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?System.out.println("===5=="); ?? ??? ??? ?this.release(); ?? ??? ??? ? ?? ??? ?} ?? ?} ?? ?private String getString() { ?? ??? ?String dataString =""; ?? ??? ?try { ?? ??? ??? ?dataString = this.bf.readLine(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?System.out.println("===6=="); ?? ??? ??? ?this.release(); ?? ??? ?} ?? ??? ?return dataString; ?? ?} ?? ? ?? ?@Override ?? ?public void run() { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?while(isRuning) { ?? ??? ??? ?String data = getString(); ?? ??? ??? ?if(!data.equals("")) { ?? ??? ??? ??? ?send(data); ?? ??? ??? ?}else { ?? ??? ??? ??? ?//send("不能發(fā)送空消息"); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ?} ? }
(2)接收封裝類
package networkCoding; ? import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; ? public class Receive implements Runnable { ?? ?private DataInputStream dis; ?? ?private Socket client; ?? ?private boolean isRuning; ?? ?public Receive(Socket client) { ?? ??? ?this.client=client; ?? ??? ?this.isRuning=true; ?? ??? ?try { ?? ??? ??? ?this.dis = new DataInputStream(client.getInputStream()); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// 出錯釋放資源 ?? ??? ??? ?System.out.println("===6=="); ?? ??? ??? ?this.release(); ?? ??? ??? ?this.isRuning=false; ?? ??? ?} ?? ?} ?? ?private String receive() { ?? ??? ?String data=""; ?? ??? ?try { ?? ??? ??? ?data= dis.readUTF(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?System.out.println("===7=="); ?? ??? ??? ?this.release(); ?? ??? ?} ?? ??? ?return data; ?? ?} ?? ?private void release() { ?? ??? ?this.isRuning=false; ?? ??? ?Utils.close(dis,client); ?? ?} ?? ?@Override ?? ?public void run() { ?? ??? ?while(isRuning) { ?? ??? ??? ?String data = receive(); ?? ??? ??? ?if(!data.equals("")) { ?? ??? ??? ??? ?System.out.println(data); ?? ??? ??? ?}else { ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ??? ? ?? ?} ? ?? ? ? }
3、工具類
package networkCoding; ? import java.io.Closeable; import java.io.IOException; ? public class Utils { ?? ?public static void main(String[] args) { ?? ??? ? ?? ?} ?? ?public static void close(Closeable...target) { ?? ??? ?for (Closeable obj : target) { ?? ??? ??? ?try { ?? ??? ??? ??? ?if(null!=obj) { ?? ??? ??? ??? ??? ?obj.close(); ?? ??? ??? ??? ?} ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springMVC框架下JQuery傳遞并解析Json數(shù)據(jù)
json作為一種輕量級的數(shù)據(jù)交換格式,在前后臺數(shù)據(jù)交換中占據(jù)著非常重要的地位,這篇文章主要介紹了springMVC框架下JQuery傳遞并解析Json數(shù)據(jù),有興趣的可以了解一下。2017-01-01解析Java編程中對于包結(jié)構(gòu)的命名和訪問
這篇文章主要介紹了Java編程中對于包結(jié)構(gòu)的命名和訪問,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-12-12springboot利用@Aspect實現(xiàn)日志工具類的詳細(xì)代碼
這篇文章主要介紹了springboot利用@Aspect實現(xiàn)日志工具類,通過實例代碼介紹了導(dǎo)包及在啟動類上進(jìn)行注解自動掃描的方法,需要的朋友可以參考下2022-03-03java字節(jié)碼框架ASM操作字節(jié)碼的方法淺析
這篇文章主要給大家介紹了關(guān)于java字節(jié)碼框架ASM如何操作字節(jié)碼的相關(guān)資料,文中通過示例代碼介紹的很詳細(xì),有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01SpringMVC Mybatis配置多個數(shù)據(jù)源并切換代碼詳解
這篇文章主要介紹了SpringMVC Mybatis配置多個數(shù)據(jù)源并切換代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11SpringMvc根據(jù)返回值類型不同處理響應(yīng)的方法
這篇文章主要介紹了SpringMvc根據(jù)返回值類型不同處理響應(yīng),我們可以通過控制器方法的返回值設(shè)置跳轉(zhuǎn)的視圖,控制器支持如void,String,ModelAndView類型,需要的朋友可以參考下2023-09-09java代理模式(靜態(tài)代理、動態(tài)代理、cglib代理)
代理(Proxy)是一種設(shè)計模式,提供了對目標(biāo)對象另外的訪問方式;這篇文章主要介紹了Java 中的三種代理模式,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07Matplotlib可視化之自定義顏色繪制精美統(tǒng)計圖
matplotlib提供的所有繪圖都帶有默認(rèn)樣式.雖然這可以進(jìn)行快速繪圖,但有時可能需要自定義繪圖的顏色和樣式,以對繪制更加精美、符合審美要求的圖像.matplotlib的設(shè)計考慮到了此需求靈活性,很容易調(diào)整matplotlib圖形的樣式,需要的朋友可以參考下2021-06-06