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

溫馨提示×

java如何實現網絡聊天室

小億
82
2024-09-26 04:13:10
欄目: 編程語言

在Java中實現網絡聊天室,你可以使用套接字編程(Socket Programming)

  1. 創建服務器端:
import java.io.*;
import java.net.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {
        int port = 12345;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Server is listening on port " + port);

        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("New client connected");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String message;
            while ((message = in.readLine()) != null) {
                System.out.println("Received: " + message);
                broadcastMessage(out, message);
            }

            socket.close();
            System.out.println("Client disconnected");
        }
    }

    private static void broadcastMessage(PrintWriter out, String message) {
        for (Socket socket : clients) {
            if (socket != null && !socket.isClosed()) {
                out.println(message);
            }
        }
    }

    private static List<Socket> clients = new ArrayList<>();
}
  1. 創建客戶端:
import java.io.*;
import java.net.*;

public class ChatClient {
    public static void main(String[] args) throws IOException {
        String serverAddress = "localhost";
        int port = 12345;

        Socket socket = new Socket(serverAddress, port);
        System.out.println("Connected to server");

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String userInput;

        while (true) {
            System.out.println("Enter your message: ");
            userInput = stdIn.readLine();
            if (userInput != null && !userInput.isEmpty()) {
                out.println(userInput);
                broadcastMessage(in, userInput + " (" + socket.getInetAddress() + ")");
            }
        }
    }

    private static void broadcastMessage(BufferedReader in, String message) throws IOException {
        for (Socket socket : clients) {
            if (socket != null && !socket.isClosed()) {
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                out.println(message);
            }
        }
    }
}

這個示例中,服務器端監聽12345端口,當客戶端連接時,服務器將接收到的消息廣播給所有連接的客戶端。客戶端可以發送消息給服務器,然后服務器將消息廣播給其他客戶端。

請注意,這個示例僅用于演示目的,實際應用中可能需要考慮更多的功能和優化,例如多個頻道、用戶身份驗證等。

0
苏州市| 宁国市| 汪清县| 温泉县| 景泰县| 屏南县| 赤水市| 安远县| 墨江| 扶风县| 宜丰县| 庄浪县| 临湘市| 集贤县| 和顺县| 长寿区| 北辰区| 威海市| 苏尼特左旗| 麻城市| 辛集市| 江都市| 威信县| 阿拉尔市| 屯门区| 新乡县| 东乌珠穆沁旗| 繁峙县| 临漳县| 太白县| 临颍县| 东台市| 桦川县| 尚志市| 三原县| 阿勒泰市| 广宁县| 永善县| 望谟县| 抚远县| 基隆市|