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

java實(shí)現(xiàn)一個簡單TCPSocket聊天室功能分享

 更新時間:2020年07月28日 15:20:07   作者:小小小丑  
這篇文章主要為大家分享了java實(shí)現(xiàn)的一個簡單TCPSocket聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)TCPSocket聊天室功能的相關(guān)代碼,供大家參考,具體內(nèi)容如下

1.TCPserver.java

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class TCPserver{
 private static final int SERVERPORT = 8888;
 private ServerSocket MyServer = null;
 private List<Socket> Clients = new ArrayList<Socket>();
 private ExecutorService mExecutorService;
 public TCPserver(){
  try{
   MyServer = new ServerSocket(SERVERPORT);
   mExecutorService = Executors.newCachedThreadPool(); 
   System.out.println("start:");
   Socket MySocket = null;
   while(true){
   MySocket = MyServer.accept();
   Clients.add(MySocket);
   mExecutorService.execute(new ThreadServer(MySocket)); 
   }
  }catch(Exception e){
    e.printStackTrace(); 
    System.exit(0);
   }
 }
 class ThreadServer implements Runnable{
  private Socket msocket=null;
  private BufferedReader in= null;
  private PrintWriter out = null;
  private String mStrMSG = null; 
  public ThreadServer(Socket socket) {
   try{   
   this.msocket=socket;
   in = new BufferedReader(new InputStreamReader(msocket.getInputStream(), "GB2312"));
   mStrMSG = "user:" + msocket.getInetAddress() + " come total:" + Clients.size(); 
   SendMassage();
   }catch(IOException e){
    System.out.println("erorr");
    System.exit(0);
   }
  }
  private void SendMassage(){
   try{
    System.out.println(mStrMSG);
    for(Socket MySocket:Clients)
    {
    out = new PrintWriter(new OutputStreamWriter(MySocket.getOutputStream(),"GB2312"),true);
    out.println(mStrMSG);
    }
   }catch(IOException e){
    System.out.println("erorr");
    System.exit(0);
   }
  }
  public void run(){
   try{
    while((mStrMSG = in.readLine())!=null){
     if(mStrMSG.trim().equals("exit")){
      Clients.remove(msocket);
      in.close();
      out.close();
      mStrMSG = "user:" + msocket.getInetAddress() + " exit tatal:" + Clients.size();      ;
      msocket.close();
      SendMassage();      
      break; 
     } 
     else{
      mStrMSG = msocket.getInetAddress()+":" + mStrMSG; 
      SendMassage();
     }
     
    }
   }catch(IOException e){
    System.out.println("erorr");
    System.exit(0);
   }
    
  }
 }
 public static void main(String[] args){
  new TCPserver();
 }
}

2.TCPclient.java

import java.net.*;
import java.io.*;
import java.util.concurrent.*;
public class TCPclient {
 private static final int PORT = 8888;
 private Socket Client = null;
 private BufferedReader sin = null;
 private ExecutorService mExecutorService;
 public TCPclient(){
  try{
   Client = new Socket("120.27.126.174",PORT);
  sin = new BufferedReader(new InputStreamReader(Client.getInputStream(),"GB2312"));
  mExecutorService = Executors.newCachedThreadPool();
  mExecutorService.execute(new ThreadClient(Client)); 
  String msg = null;
  while((msg = sin.readLine()) != null) { 
    System.out.println(msg); 
   } 
  }catch(IOException e){
      System.out.println(e.getMessage()); 
    }
   
 }
 class ThreadClient extends Thread{
  private Socket mSocket = null;
  private String msg = null;
  BufferedReader in = null;
  PrintWriter out = null;
   public ThreadClient(Socket socket){
    this.mSocket = socket;
   }
   public void run(){
    try{
     in = new BufferedReader(new InputStreamReader(System.in));
     out = new PrintWriter(new OutputStreamWriter(mSocket.getOutputStream(), "GB2312"), true);
     while(true){
     msg = in.readLine();
     out.println(msg);
      if(msg.trim().equals("exit")){
       in.close();
       out.close();
        mExecutorService.shutdownNow();    
      break;
      }
     }
    }catch(IOException e){
     System.out.println("see you");
     System.exit(0);
    }
   }
 }
 public static void main(String[] args){
  new TCPclient();
 }
}

以上就是java實(shí)現(xiàn)TCPSocket聊天室功能的代碼,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • java?并發(fā)線程個數(shù)的如何確定

    java?并發(fā)線程個數(shù)的如何確定

    這篇文章主要介紹了java?并發(fā)線程個數(shù)的如何確定,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java構(gòu)造函數(shù)的三種類型總結(jié)

    java構(gòu)造函數(shù)的三種類型總結(jié)

    在本篇文章里小編給大家整理了一篇關(guān)于java構(gòu)造函數(shù)的三種類型總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • 解決線程并發(fā)redisson使用遇到的坑

    解決線程并發(fā)redisson使用遇到的坑

    這篇文章主要介紹了解決線程并發(fā)redisson使用遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中的List與Set轉(zhuǎn)換方式

    Java中的List與Set轉(zhuǎn)換方式

    Java中,List和Set是兩種基本的集合類型,它們在允許重復(fù)元素、元素順序、實(shí)現(xiàn)類以及性能方面有著明顯的區(qū)別,List允許重復(fù)元素并保持元素插入的順序,常見實(shí)現(xiàn)有ArrayList、LinkedList和Vector;Set不允許重復(fù)元素
    2024-11-11
  • Java實(shí)現(xiàn)短信驗證碼的示例代碼

    Java實(shí)現(xiàn)短信驗證碼的示例代碼

    Java是一種流行的編程語言,驗證碼是一種常用的網(wǎng)絡(luò)安全技術(shù)。Java發(fā)展至今,網(wǎng)上也出現(xiàn)了各種各樣的驗證碼,下面是用Java實(shí)現(xiàn)短信驗證碼的總結(jié),感興趣的可以了解一下
    2023-03-03
  • Java實(shí)現(xiàn)MD5加密算法方法例子

    Java實(shí)現(xiàn)MD5加密算法方法例子

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)MD5加密算法方法的相關(guān)資料,MD5加密是一種常見的加密方式,我們經(jīng)常用在保存用戶密碼和關(guān)鍵信息上,需要的朋友可以參考下
    2023-10-10
  • SpringData?JPA的常用語法匯總

    SpringData?JPA的常用語法匯總

    Spring Data JPA是Spring基于ORM框架、JPA規(guī)范的基礎(chǔ)上封裝的一套JPA應(yīng)用框架,可使開發(fā)者用極簡的代碼即可實(shí)現(xiàn)對數(shù)據(jù)的訪問和操作,下面這篇文章主要給大家介紹了關(guān)于SpringData?JPA的常用語法,需要的朋友可以參考下
    2022-06-06
  • java構(gòu)建樹形結(jié)構(gòu)的方式及如何組裝樹狀結(jié)構(gòu)數(shù)據(jù)

    java構(gòu)建樹形結(jié)構(gòu)的方式及如何組裝樹狀結(jié)構(gòu)數(shù)據(jù)

    這篇文章主要介紹了在Java中構(gòu)建樹狀數(shù)據(jù)結(jié)構(gòu)的幾種常見方法,包括遞歸、使用Map/HashMap以及基于Stream流的方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • 盤點(diǎn)Java中延時任務(wù)的多種實(shí)現(xiàn)方式

    盤點(diǎn)Java中延時任務(wù)的多種實(shí)現(xiàn)方式

    當(dāng)需要一個定時發(fā)布系統(tǒng)通告的功能,如何實(shí)現(xiàn)??當(dāng)支付超時,訂單自動取消,如何實(shí)現(xiàn)?其實(shí)這些問題本質(zhì)都是延時任務(wù)的實(shí)現(xiàn),本文為大家盤點(diǎn)了多種常見的延時任務(wù)實(shí)現(xiàn)方法,希望對大家有所幫助
    2022-12-12
  • Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時數(shù)據(jù)展示

    Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時數(shù)據(jù)展示

    本文主要介紹了Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時數(shù)據(jù)展示
    2024-06-06

最新評論

伊川县| 西乌珠穆沁旗| 清水河县| 垦利县| 绵阳市| 仲巴县| 保康县| 海宁市| 黑龙江省| 东平县| 呼玛县| 元氏县| 台南市| 潼南县| 乐昌市| 磐安县| 乌苏市| 堆龙德庆县| 宁夏| 苍溪县| 柳江县| 辽源市| 小金县| 平顺县| 饶平县| 内丘县| 泰安市| 石嘴山市| 什邡市| 梅州市| 台东县| 塔河县| 永顺县| 富宁县| 铜鼓县| 博湖县| 唐海县| 奉化市| 襄城县| 阳西县| 达孜县|