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

溫馨提示×

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
遂川县| 临潭县| 洛宁县| 兴仁县| 蒲江县| 怀宁县| 涞水县| 石门县| 普兰店市| 故城县| 仪陇县| 卢龙县| 连城县| 平果县| 荔波县| 阿拉尔市| 修水县| 改则县| 东乡族自治县| 济源市| 乐平市| 亳州市| 平阳县| 靖江市| 建宁县| 巴林右旗| 林口县| 彩票| 湄潭县| 崇明县| 米林县| 胶南市| 北川| 兴海县| 莱芜市| 定州市| 定日县| 绥中县| 太康县| 东乌| 龙江县|