溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java SE實現(xiàn)多人聊天室功能

發(fā)布時間:2020-09-29 19:20:31 來源:腳本之家 閱讀:250 作者:L_X_Y_HH 欄目:編程語言

本文實例為大家分享了Java SE實現(xiàn)多人聊天室功能的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)功能:

1.實現(xiàn)用戶注冊上線,下線

2.實現(xiàn)群聊和私聊功能

3.實現(xiàn)統(tǒng)計當(dāng)前在線人數(shù)

實現(xiàn)思路:

1.首先,要實現(xiàn)服務(wù)端與客戶端之間的連接

這里是使用套接字建立TCP連接:

(1)服務(wù)器端先實例化一個描述服務(wù)器端口號的ServerSocket對象

(2)客戶端要創(chuàng)建Socket對象來連接指定的服務(wù)器端

(3)服務(wù)器端調(diào)用ServerSocket類的accept()方法來監(jiān)聽連接到服務(wù)器端的客戶端信息

(4)若服務(wù)器端與客戶端連接成功,雙方將返回一個Socket對象,此時雙方可以進(jìn)行通信

(5)服務(wù)器端與客戶端使用I/O流進(jìn)行連接,服務(wù)端的輸出流連接客戶端的輸入流,客戶端的輸出流連接服務(wù)端的輸入流

(6)使用close()方法關(guān)閉套接字(一定要記得關(guān)閉)

2.因為是擁有一個服務(wù)端來實現(xiàn)多個客戶端的連接,此處還要解決的是多線程的問題。

每個客戶端需要兩個線程,來分別處理向服務(wù)端發(fā)送消息和向服務(wù)端接收消息

而服務(wù)端,當(dāng)每增加一個客戶端與服務(wù)端連接,服務(wù)端都要多創(chuàng)建一個線程來處理與客戶端的連接

具體代碼:

單線程實現(xiàn):

客戶端

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
 
/**
 * @author LXY
 * @email 403824215@qq.com
 * @date 2018/7/20 15:46
 */
//客戶端
public class SingleClient {
  public static void main(String[] args) throws IOException {
    //客戶端連接服務(wù)器,返回套接字Socket對象
    Socket socket = new Socket("127.0.0.1",6666);
    //獲取服務(wù)端的輸出流,向服務(wù)器端輸出內(nèi)容
    PrintStream printStream = new PrintStream(socket.getOutputStream());
    printStream.println("我是客戶端" + socket.getLocalPort());
    //獲取服務(wù)器端的輸入流,讀取服務(wù)器端的內(nèi)容
    Scanner scanner = new Scanner(socket.getInputStream());
    scanner.useDelimiter("\n");
    if(scanner.hasNext())
    {
      System.out.println(scanner.next());
    }
    //關(guān)閉流
    socket.close();
  }
}

服務(wù)端

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
 
/**
 * @author LXY
 * @email 403824215@qq.com
 * @date 2018/7/20 15:34
 */
//服務(wù)端
public class SingleServer {
  public static void main(String[] args) throws IOException {
    //創(chuàng)建服務(wù)器端的ServerSocket對象,等待客戶端進(jìn)行連接
    ServerSocket serverSocket = new ServerSocket(6666);
    System.out.println("服務(wù)器的端口號為6666,等待客戶端連接。。。");
    //偵聽并接收服務(wù)器端的連接,返回套接字Socket對象
    Socket socket = serverSocket.accept();
    //獲取客戶端的輸入流,讀取客戶端的輸入內(nèi)容
    Scanner scanner = new Scanner(socket.getInputStream());
    scanner.useDelimiter("\n");
    if(scanner.hasNext())
    {
      System.out.println("客戶端發(fā)來消息:" + scanner.next());
    }
    //獲取客戶端的輸出流,向客戶端輸出內(nèi)容
    PrintStream printStream = new PrintStream(socket.getOutputStream());
    printStream.println("客戶端你好,我是服務(wù)器端:" + serverSocket.getLocalPort());
    //關(guān)閉流
    serverSocket.close();
  }
}

多線程實現(xiàn)

客戶端

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
 
/**
 * @author LXY
 * @email 403824215@qq.com
 * @date 2018/7/20 15:55
 */
//客戶端
 
//客戶端讀取服務(wù)器端信息的線程
class ClientReadServer implements Runnable
{
  private Socket socket;
 
  public ClientReadServer(Socket socket)
  {
    this.socket = socket;
  }
 
  public void run() {
    try {
      Scanner scanner = new Scanner(socket.getInputStream());
      while (scanner.hasNext())
      {
        System.out.println(scanner.next());
      }
      socket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 
 
//客戶端向服務(wù)端發(fā)送信息的線程
class ClientSendServer implements Runnable
{
  private Socket socket;
 
  public ClientSendServer(Socket socket)
  {
    this.socket = socket;
  }
 
  public void run() {
    try {
      PrintStream printStream = new PrintStream(socket.getOutputStream());
      Scanner scanner = new Scanner(System.in);
      while (true)
      {
        String msg = null;
        if(scanner.hasNext())
        {
          msg = scanner.next();
          printStream.println(msg);
        }
        if(msg.equals("bye"))
        {
          scanner.close();
          printStream.close();
          break;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
 
  }
}
 
public class MultiClient {
  public static void main(String[] args) throws IOException {
    Socket socket = new Socket("127.0.0.1",6666);
    Thread read = new Thread(new ClientReadServer(socket));
    Thread send = new Thread(new ClientSendServer(socket));
    read.start();
    send.start();
  }
}

服務(wù)端

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author LXY
 * @email 403824215@qq.com
 * @date 2018/7/20 16:12
 */
class Server implements Runnable
{
  private static Map<String,Socket> map = new ConcurrentHashMap<String, Socket>();
 
  private Socket socket;
 
  public Server(Socket socket)
  {
    this.socket = socket;
  }
 
  public void run() {
    try {
      Scanner scanner = new Scanner(socket.getInputStream());
      String msg = null;
      while (true)
      {
        if(scanner.hasNextLine())
        {
          msg = scanner.nextLine();
          Pattern pattern = Pattern.compile("\r");
          Matcher matcher = pattern.matcher(msg);
          msg = matcher.replaceAll("");
          //用戶注冊——格式:userName:用戶名
          if(msg.startsWith("userName:"))
          {
            String userName = msg.split("\\:")[1];
            userRegist(userName,socket);
            continue;
          }
          //群聊——格式:G:群聊信息
          else if(msg.startsWith("G:"))
          {
            firstStep(socket);
            String str = msg.split("\\:")[1];
            groupChat(socket,str);
            continue;
          }
          else if(msg.startsWith("P:") && msg.contains("-"))
          {
            firstStep(socket);
            String userName = msg.split("\\:")[1].split("-")[0];
            String str = msg.split("\\:")[1].split("-")[1];
            privateChat(socket,userName,str);
            continue;
          }
          else if(msg.contains("bye"))
          {
            firstStep(socket);
            userExit(socket);
            continue;
          }
          else
          {
            PrintStream printStream = new PrintStream(socket.getOutputStream());
            printStream.println("格式輸入錯誤");
            continue;
 
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
 
  }
 
  private void firstStep(Socket socket) throws IOException {
    Set<Map.Entry<String,Socket>> set = map.entrySet();
    for(Map.Entry<String,Socket> entry:set)
    {
      if(entry.getValue().equals(socket))
      {
        if(entry.getValue() == null)
        {
          PrintStream printStream = new PrintStream(socket.getOutputStream());
          printStream.println("請先進(jìn)行注冊操作!格式為:[userName:用戶名]");
        }
      }
    }
  }
 
  private void userRegist(String userName, Socket socket) {
    map.put(userName,socket);
    System.out.println("用戶名:" + userName + "客戶端" + socket +"上線了!!");
    System.out.println("當(dāng)前在線人數(shù)為" + map.size() + "人");
  }
 
  private void groupChat(Socket socket, String msg) throws IOException {
    Set<Map.Entry<String,Socket>> set = map.entrySet();
    String userName = null;
    for(Map.Entry<String,Socket> entry:set)
    {
      if(entry.getValue().equals(socket))
      {
        userName = entry.getKey();
        break;
      }
    }
    for(Map.Entry<String,Socket> entry:set)
    {
      Socket client = entry.getValue();
      PrintStream printStream = new PrintStream(client.getOutputStream());
      printStream.println(userName + "說" + msg);
    }
  }
 
  private void privateChat(Socket socket, String userName, String msg) throws IOException {
    String curUser = null;
    Set<Map.Entry<String,Socket>> set = map.entrySet();
    for(Map.Entry<String,Socket> entry:set)
    {
      if(entry.getValue().equals(socket))
      {
        curUser = entry.getKey();
        break;
      }
    }
    Socket client = map.get(userName);
    PrintStream printStream = new PrintStream(client.getOutputStream());
    printStream.println(curUser + "私聊說" + msg);
  }
 
  private void userExit(Socket socket) {
    String userName = null;
    for(String key:map.keySet())
    {
      if(map.get(key).equals(socket))
      {
        userName = key;
        break;
      }
    }
    map.remove(userName,socket);
    System.out.println("用戶" + userName + "已下線");
  }
}
 
 
public class MultiServer {
  public static void main(String[] args) {
    try {
      ServerSocket serverSocket = new ServerSocket(6666);
      //使用線程池
      ExecutorService executorService = Executors.newFixedThreadPool(20);
      for(int i = 0;i < 20;i++)
      {
        System.out.println("歡迎來到聊天室。。。");
        Socket socket = serverSocket.accept();
        System.out.println("新人加入。。。");
        executorService.execute(new Server(socket));
      }
      executorService.shutdown();
      serverSocket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
 
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI