欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(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聊天室功能的代碼,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Java基礎(chǔ)將Bean屬性值放入Map中的實(shí)例

    Java基礎(chǔ)將Bean屬性值放入Map中的實(shí)例

    這篇文章主要介紹了Java基礎(chǔ)將Bean屬性值放入Map中的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 詳解SpringBoot構(gòu)建Docker鏡像的3種方式

    詳解SpringBoot構(gòu)建Docker鏡像的3種方式

    這篇文章主要介紹了SpringBoot構(gòu)建Docker鏡像的3種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 解決Springboot-application.properties中文亂碼問題

    解決Springboot-application.properties中文亂碼問題

    這篇文章主要介紹了解決Springboot-application.properties中文亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 詳解Java的類加載機(jī)制及熱部署的原理

    詳解Java的類加載機(jī)制及熱部署的原理

    今天我要講的就是Java的熱部署的原理,由于熱部署的原理和類的加載機(jī)制有關(guān),所以打算講一下類加載的機(jī)制,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2021-05-05
  • Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例

    Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Java泛型之類型擦除實(shí)例詳解

    Java泛型之類型擦除實(shí)例詳解

    Java泛型在使用過程有諸多的問題,如不存在List<String>.class,List<Integer>不能賦值給List<Number>(不可協(xié)變),奇怪的ClassCastException等,這篇文章主要給大家介紹了關(guān)于Java泛型之類型擦除的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)

    springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)

    這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能,比較基礎(chǔ)的學(xué)習(xí)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java創(chuàng)建多線程局域網(wǎng)聊天室實(shí)例

    Java創(chuàng)建多線程局域網(wǎng)聊天室實(shí)例

    這篇文章主要介紹了Java創(chuàng)建多線程局域網(wǎng)聊天室實(shí)例,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • springboot實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定

    springboot實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定

    這篇文章主要介紹了springboot實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 如何將Object類轉(zhuǎn)換為實(shí)體類

    如何將Object類轉(zhuǎn)換為實(shí)體類

    這篇文章主要介紹了如何將Object類轉(zhuǎn)換為實(shí)體類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論