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

溫馨提示×

怎么用java實現一個簡易的聊天室

小億
139
2023-10-31 19:38:36
欄目: 編程語言

要實現一個簡易的聊天室,可以使用Java的Socket編程實現。下面是一個簡單的實現示例:

服務器端代碼:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ChatServer {
    private ServerSocket serverSocket;
    private List<ClientHandler> clients = new ArrayList<>();

    public static void main(String[] args) {
        ChatServer server = new ChatServer();
        server.start(8888);
    }

    public void start(int port) {
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("服務器已啟動,監聽端口:" + port);

            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客戶端已連接:" + clientSocket.getRemoteSocketAddress());

                // 創建一個新的客戶端處理器
                ClientHandler clientHandler = new ClientHandler(clientSocket);
                clients.add(clientHandler);
                clientHandler.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void broadcast(String message, ClientHandler excludeClient) {
        for (ClientHandler client : clients) {
            if (client != excludeClient) {
                client.sendMessage(message);
            }
        }
    }

    public synchronized void removeClient(ClientHandler client) {
        clients.remove(client);
    }

    private class ClientHandler extends Thread {
        private Socket clientSocket;
        private InputStream inputStream;
        private OutputStream outputStream;

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

        @Override
        public void run() {
            try {
                inputStream = clientSocket.getInputStream();
                outputStream = clientSocket.getOutputStream();

                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    String message = new String(buffer, 0, bytesRead);
                    System.out.println("收到消息:" + message);

                    // 廣播消息給其他客戶端
                    broadcast(message, this);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    clientSocket.close();
                    removeClient(this);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public void sendMessage(String message) {
            try {
                outputStream.write(message.getBytes());
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

客戶端代碼:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient {
    private Socket socket;
    private InputStream inputStream;
    private OutputStream outputStream;

    public static void main(String[] args) {
        ChatClient client = new ChatClient();
        client.connect("localhost", 8888);
    }

    public void connect(String serverAddress, int serverPort) {
        try {
            socket = new Socket(serverAddress, serverPort);
            System.out.println("已連接到服務器:" + serverAddress + ":" + serverPort);

            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();

            // 接收和發送消息的線程
            Thread receiveThread = new Thread(this::receiveMessages);
            Thread sendThread = new Thread(this::sendMessages);

            receiveThread.start();
            sendThread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void receiveMessages() {
        try {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                String message = new String(buffer, 0, bytesRead);
                System.out.println(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessages() {
        Scanner scanner = new Scanner(System.in);
        try {
            while (true) {
                String message = scanner.nextLine();
                outputStream.write(message.getBytes());
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在命令行中,先運行服務器端的ChatServer類,再運行客戶端的ChatClient類。客戶端可以輸入消息,然后服務器端會將消息廣播給其他客戶端。注意要將客戶端的connect方法中的serverAddress改為服務器的IP地址。

0
蓬安县| 鹤山市| 师宗县| 肇庆市| 双峰县| 抚远县| 塔城市| 岑溪市| 公主岭市| 青铜峡市| 南郑县| 辽宁省| 保山市| 和林格尔县| 宜昌市| 松潘县| 漠河县| 信丰县| 金昌市| 扶绥县| 罗江县| 合山市| 弥渡县| 泸定县| 米林县| 定结县| 堆龙德庆县| 天祝| 泰来县| 延津县| 即墨市| 永年县| 涞源县| 贵南县| 荥经县| 滨海县| 太仆寺旗| 米泉市| 尤溪县| 定边县| 扬中市|