亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

java如何實現一個簡易的聊天室

小億
85
2024-08-01 20:42:17
欄目: 編程語言

要實現一個簡易的聊天室,可以使用Socket編程來實現客戶端和服務端之間的通信。以下是一個簡單的Java聊天室的實現步驟:

  1. 創建一個Server端:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ChatServer {
    private static List<PrintWriter> clients = new ArrayList<>();

    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(8080)) {
            System.out.println("Server started...");

            while (true) {
                Socket clientSocket = serverSocket.accept();
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                clients.add(out);
                
                Thread t = new Thread(new ClientHandler(clientSocket));
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void broadcast(String message) {
        for (PrintWriter client : clients) {
            client.println(message);
        }
    }

    private static class ClientHandler implements Runnable {
        private Socket clientSocket;
        private Scanner in;

        public ClientHandler(Socket socket) {
            this.clientSocket = socket;
        }

        @Override
        public void run() {
            try {
                in = new Scanner(clientSocket.getInputStream());

                while (in.hasNextLine()) {
                    String message = in.nextLine();
                    broadcast(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                clients.remove(new PrintWriter(clientSocket.getOutputStream()));
            }
        }
    }
}
  1. 創建一個Client端:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 8080);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             Scanner in = new Scanner(socket.getInputStream());
             Scanner scanner = new Scanner(System.in)) {
            System.out.println("Connected to server. Start typing messages:");

            Thread t = new Thread(() -> {
                while (in.hasNextLine()) {
                    System.out.println(in.nextLine());
                }
            });
            t.start();

            while (true) {
                String message = scanner.nextLine();
                out.println(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

這樣就可以實現一個簡單的聊天室,Server端負責接受客戶端連接并廣播消息,Client端負責發送和接收消息。可以通過運行多個Client端來進行聊天。

0
赫章县| 邵阳市| 永和县| 长乐市| 石楼县| 乌拉特后旗| 宿松县| 衢州市| 九龙城区| 保定市| 旬阳县| 平山县| 永平县| 区。| 新田县| 调兵山市| 正安县| 达拉特旗| 江津市| 平遥县| 兴业县| 喀什市| 天峨县| 镇坪县| 泽州县| 涞源县| 西乌| 陈巴尔虎旗| 祁东县| 遂宁市| 平安县| 灵台县| 微博| 延川县| 红河县| 商洛市| 潮州市| 武宁县| 河津市| 喜德县| 额济纳旗|