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

溫馨提示×

Java里socket連接怎樣進行心跳檢測

小樊
93
2024-09-30 00:45:28
欄目: 編程語言

在Java中,可以使用Socket連接進行心跳檢測。心跳檢測的主要目的是檢測對方是否在線,通過定期發送小數據包來確認連接是否有效。以下是一個簡單的心跳檢測示例:

  1. 服務器端代碼:
import java.io.*;
import java.net.*;
import java.util.concurrent.*;

public class Server {
    public static void main(String[] args) throws IOException, InterruptedException {
        int port = 12345;
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Server started on port " + port);

        while (true) {
            Socket clientSocket = serverSocket.accept();
            executorService.submit(() -> handleClient(clientSocket));
        }
    }

    private static void handleClient(Socket clientSocket) {
        try {
            InputStream inputStream = clientSocket.getInputStream();
            OutputStream outputStream = clientSocket.getOutputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            PrintWriter writer = new PrintWriter(outputStream, true);

            String heartBeatMessage = "HEARTBEAT";
            ScheduledExecutorService heartbeatExecutor = Executors.newScheduledThreadPool(1);
            heartbeatExecutor.scheduleAtFixedRate(() -> {
                try {
                    writer.println(heartBeatMessage);
                } catch (IOException e) {
                    System.out.println("Error sending heartbeat message: " + e.getMessage());
                    break;
                }
            }, 0, 30, TimeUnit.SECONDS); // 每30秒發送一次心跳檢測

            String inputLine;
            while ((inputLine = reader.readLine()) != null) {
                System.out.println("Received message from client: " + inputLine);
                if (inputLine.equals("BYE")) {
                    break;
                }
            }

            heartbeatExecutor.shutdown();
            clientSocket.close();
        } catch (IOException e) {
            System.out.println("Error handling client: " + e.getMessage());
        }
    }
}
  1. 客戶端代碼:
import java.io.*;
import java.net.*;
import java.util.concurrent.*;

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

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

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

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        PrintWriter writer = new PrintWriter(outputStream, true);

        String heartBeatMessage = "HEARTBEAT";
        ScheduledExecutorService heartbeatExecutor = Executors.newScheduledThreadPool(1);
        heartbeatExecutor.scheduleAtFixedRate(() -> {
            try {
                writer.println(heartBeatMessage);
            } catch (IOException e) {
                System.out.println("Error sending heartbeat message: " + e.getMessage());
                break;
            }
        }, 0, 30, TimeUnit.SECONDS); // 每30秒發送一次心跳檢測

        String inputLine;
        while ((inputLine = reader.readLine()) != null) {
            System.out.println("Received message from server: " + inputLine);
            if (inputLine.equals("BYE")) {
                break;
            }
        }

        heartbeatExecutor.shutdown();
        socket.close();
    }
}

在這個示例中,服務器和客戶端都定期發送心跳檢測消息(“HEARTBEAT”),以確認連接是否有效。如果一方在指定的時間內沒有收到心跳檢測消息,它會認為連接已經斷開,并采取相應的措施。

0
五莲县| 新平| 湘潭市| 中西区| 阳东县| 郑州市| 蓬溪县| 霍州市| 雷州市| 永定县| 伊吾县| 永康市| 河池市| 蒲城县| 绩溪县| 吴堡县| 濮阳县| 开远市| 施秉县| 塘沽区| 黑山县| 东乌珠穆沁旗| 苍山县| 永新县| 石首市| 武夷山市| 牙克石市| 扶余县| 犍为县| 济南市| 奈曼旗| 大连市| 定日县| 平度市| 沙湾县| 诏安县| 万全县| 德阳市| 永和县| 新蔡县| 钟山县|