91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
长武县| 德庆县| 肇源县| 内丘县| 宁海县| 湖州市| 祥云县| 青神县| 义马市| 辽源市| 泽州县| 资阳市| 周口市| 秦皇岛市| 丰都县| 洪湖市| 平乡县| 黄山市| 吴忠市| 衢州市| 黔江区| 花莲县| 汉沽区| 洮南市| 通江县| 志丹县| 哈密市| 芦山县| 临澧县| 英山县| 密山市| 阿克陶县| 额济纳旗| 建德市| 大洼县| 南通市| 谷城县| 饶河县| 广西| 沙坪坝区| 武平县|