Java 判断IP和端口可达

在网络通信中,判断一个IP地址和端口是否可达是一项常见的任务。Java提供了多种方式来实现这个功能,本文将介绍几种常用的方法。

使用Socket类进行判断

Java的Socket类提供了非常简洁的方式来进行IP和端口的可达性判断。下面是一个示例代码:

import java.net.*;

public class SocketExample {
    public static void main(String[] args) {
        String ipAddress = "127.0.0.1";
        int port = 8080;

        try {
            Socket socket = new Socket(ipAddress, port);
            System.out.println("IP地址和端口可达");
        } catch (Exception e) {
            System.out.println("IP地址和端口不可达");
        }
    }
}

在上述代码中,我们创建了一个Socket实例,并尝试连接指定的IP地址和端口。如果连接成功,则说明IP地址和端口可达;如果连接失败,则说明IP地址和端口不可达。

使用InetAddress类进行判断

Java的InetAddress类也提供了判断IP和端口可达性的方法。下面是一个示例代码:

import java.net.*;

public class InetAddressExample {
    public static void main(String[] args) {
        String ipAddress = "127.0.0.1";
        int port = 8080;

        try {
            InetAddress address = InetAddress.getByName(ipAddress);
            boolean isReachable = address.isReachable(5000);
            if (isReachable) {
                System.out.println("IP地址和端口可达");
            } else {
                System.out.println("IP地址和端口不可达");
            }
        } catch (Exception e) {
            System.out.println("IP地址和端口不可达");
        }
    }
}

在上述代码中,我们使用InetAddress类的isReachable方法来判断指定的IP地址和端口是否可达。其中,参数timeout表示超时时间,单位为毫秒。

使用Java 8的CompletableFuture进行异步判断

如果我们需要同时判断多个IP地址和端口的可达性,可以使用Java 8的CompletableFuture来实现异步判断。下面是一个示例代码:

import java.net.*;
import java.util.concurrent.*;

public class CompletableFutureExample {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        String[] ipAddresses = {"127.0.0.1", "192.168.0.1", "10.0.0.1"};
        int port = 8080;

        ExecutorService executor = Executors.newFixedThreadPool(ipAddresses.length);
        CompletableFuture<Void>[] futures = new CompletableFuture[ipAddresses.length];

        for (int i = 0; i < ipAddresses.length; i++) {
            String ipAddress = ipAddresses[i];
            futures[i] = CompletableFuture.runAsync(() -> {
                try {
                    Socket socket = new Socket(ipAddress, port);
                    System.out.println("IP地址和端口可达:" + ipAddress);
                } catch (Exception e) {
                    System.out.println("IP地址和端口不可达:" + ipAddress);
                }
            }, executor);
        }

        CompletableFuture.allOf(futures).get();
        executor.shutdown();
    }
}

在上述代码中,我们使用了CompletableFuture实现了并发的IP和端口可达性判断。首先,我们创建了一个线程池和CompletableFuture数组来存储每个IP地址和端口的判断结果。然后,使用CompletableFuture.runAsync方法将每个判断任务提交给线程池并执行。最后,使用CompletableFuture.allOf方法等待所有任务完成。

总结

本文介绍了三种常用的方法来判断IP地址和端口的可达性:使用Socket类、使用InetAddress类和使用Java 8的CompletableFuture。开发人员可以根据具体的需求选择合适的方法来进行判断。在实际应用中,我们通常会将IP和端口的可达性判断封装成工具类,以便在不同的场景中复用。

希望本文能够帮助读者理解和使用Java判断IP地址和端口可达的方法。

序列图

下面是一个序列图,展示了使用Socket类进行IP和端口可达性判断的过程:

sequenceDiagram
    participant Client
    participant Server

    Client->>Server: 建立连接
    activate Server
    Server->>Client: 连接成功