最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java網(wǎng)絡編程之群聊功能

 更新時間:2022年05月19日 14:24:56   作者:fire-fire-fox  
這篇文章主要為大家詳細介紹了java網(wǎng)絡編程之群聊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java網(wǎng)絡編程之群聊功能的具體代碼,供大家參考,具體內(nèi)容如下

1、服務端

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)建服務器
?* 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)建服務器
?? ??? ?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)建客戶端 + 服務端的地址端口號;
?* 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)建客戶端 + 服務端的地址端口號;
?? ??? ?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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA 啟動 Tomcat 項目輸出亂碼的解決方法

    IDEA 啟動 Tomcat 項目輸出亂碼的解決方法

    這篇文章主要介紹了IDEA 啟動 Tomcat 項目輸出亂碼的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • java 整型數(shù)與Integer的緩存深入理解

    java 整型數(shù)與Integer的緩存深入理解

    這篇文章主要介紹了java 整型數(shù)與Integer的緩存深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Spring?Boot?3?整合?MinIO?實現(xiàn)分布式文件存儲的全過程

    Spring?Boot?3?整合?MinIO?實現(xiàn)分布式文件存儲的全過程

    本文介紹了如何使用SpringBoot3和MinIO實現(xiàn)分布式文件存儲,通過MinIO的分布式對象存儲系統(tǒng),可以解決傳統(tǒng)單機文件存儲方案在面對大規(guī)模數(shù)據(jù)和高并發(fā)訪問時的不足,文章詳細講解了MinIO的安裝、配置和使用,感興趣的朋友一起看看吧
    2025-03-03
  • java網(wǎng)絡編程基礎(chǔ)知識介紹

    java網(wǎng)絡編程基礎(chǔ)知識介紹

    這篇文章主要介紹了java網(wǎng)絡編程基礎(chǔ)知識介紹,涉及OSI分層模型和TCP/IP分層模型的對應關(guān)系、IP地址、端口號、tcp、udp等相關(guān)內(nèi)容,還是比較不錯的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • 聊聊DecimalFormat的用法及各符號的意義

    聊聊DecimalFormat的用法及各符號的意義

    這篇文章主要介紹了DecimalFormat的用法及各符號的意義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java異常處理實例詳解

    Java異常處理實例詳解

    這篇文章主要介紹了Java異常處理實例詳解,列舉了實際例子講解的很清晰,有感興趣的同學可以學習下
    2021-03-03
  • java實現(xiàn)簡單掃雷小游戲

    java實現(xiàn)簡單掃雷小游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Spring Boot中@ConditionalOnProperty的使用方法

    Spring Boot中@ConditionalOnProperty的使用方法

    這篇文章主要給大家介紹了關(guān)于Spring Boot中@ConditionalOnProperty的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • java序列化對象根據(jù)不同配置動態(tài)改變屬性名的方法

    java序列化對象根據(jù)不同配置動態(tài)改變屬性名的方法

    本文主要介紹了java序列化對象根據(jù)不同配置動態(tài)改變屬性名的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Maven的幾個常用plugin

    Maven的幾個常用plugin

    本文主要介紹了Maven的幾個常用plugin。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論

揭西县| 赣榆县| 延吉市| 夏河县| 克拉玛依市| 闻喜县| 遂平县| 德化县| 长汀县| 灵山县| 吴旗县| 开原市| 抚松县| 金坛市| 开化县| 邯郸县| 台中县| 辽宁省| 兴化市| 沽源县| 阳原县| 堆龙德庆县| 三门县| 崇明县| 车险| 元谋县| 保德县| 故城县| 黄陵县| 宁强县| 嘉鱼县| 沁阳市| 武强县| 平邑县| 财经| 梁山县| 葵青区| 简阳市| 昌黎县| 奉新县| 资源县|