java中如何检查IP是否有效?

发布于 2024-12-18 14:55:34 字数 713 浏览 4 评论 0原文

我的网络中有 5 台具有不同 IP 地址的设备。我希望连接到这些设备,并在它们在我的网络中可用时通过 TCP/IP 套接字从它们获取数据。 我如何检查它们在java中是否可用?

public void setUpConnection() {
    try {
        Socket client = new Socket(hostIp, hostPort);
        socketReader = client.getInputStream();
        socketWriter = new PrintWriter(client.getOutputStream());
    } catch (UnknownHostException e) {
        System.out.println("Error setting up socket connection: unknown host at " +   hostIp);
        System.out.println("host: " + hostIp + "port: " + hostPort);
    } catch (IOException e) {
        System.out.println("Error setting up socket connection: " + e);
        System.out.println("host: " + hostIp + "port:" + hostPort);
    }
}

There are 5 devices in my network with different IP addresses. I wish to connect to these devices and get data from them over TCP/IP socket when they are available in my network.
How can I check if they are available in java?

public void setUpConnection() {
    try {
        Socket client = new Socket(hostIp, hostPort);
        socketReader = client.getInputStream();
        socketWriter = new PrintWriter(client.getOutputStream());
    } catch (UnknownHostException e) {
        System.out.println("Error setting up socket connection: unknown host at " +   hostIp);
        System.out.println("host: " + hostIp + "port: " + hostPort);
    } catch (IOException e) {
        System.out.println("Error setting up socket connection: " + e);
        System.out.println("host: " + hostIp + "port:" + hostPort);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落花浅忆 2024-12-25 14:55:34
InetAddress.getByName(host).isReachable(timeOut);

进一步参考此处

InetAddress.getByName(host).isReachable(timeOut);

Further reference here

半葬歌 2024-12-25 14:55:34

如果您只想检查主机是否已启动,可以使用 可到达

if you just want to check whether or not the host is up, you can use isReachable

屋顶上的小猫咪 2024-12-25 14:55:34

对于纯 Java,基本上有两种方法:

  1. Active:作为示例,但循环遍历主机上的所有端口和所有绑定 IP

  2. 被动:在预定义端口上运行一个小型服务器,另一个服务器在可用时会自行注册。

isReachable 可能会因文档中所述的多种原因而失败。

For pure Java there are basically two methods:

  1. Active: as your example, but looping thru all ports and all bound IP's on the host

  2. Passive: running a small server on predefined port(s) and the other one will register themself when they get available.

isReachable may fail for a lot of reasons as stated in the docs.

可是我不能没有你 2024-12-25 14:55:34

TCP/IP 是一种握手模式。与这 5 个设备中的任何一个建立连接。您的客户和接收客户都应该还活着。如果可能的话,首先使用Socket NIO。你能否编写客户端逻辑,以便每当任何客户端上线时,首先在数据库中输入其IP和端口,并尝试从数据库获取其他可用的IP和端口,并尝试与它们创建连接。每当任何客户端设备无法访问时,请从数据库中删除该条目。示例:当第一个设备上线时,只会创建一个套接字,因为数据库中只有一个条目,当第二个设备上线时,有两个条目,一个是该设备的 IP 和端口,第二个条目所属到最先出现的设备。

谢谢
苏尼尔·库马尔·萨胡

TCP/IP is a mode of handshaking. To make connection with any of these 5 devices. both your client and the recipient client should be alive. First if possible then use Socket NIO. can you write client logic such that whenever any of client comes online first make an entry of its ip and port in database and try to get other available ip and ports from database and try to create connection with them. whenever any client device became unreachable delete that entry from database. Example: when first device comes online then there is only one socket will be created because there is only one entry in databae when second device comes online there is two entries one is the ip and port of this device and the second entry belong to the device which came first.

Thanks
Sunil Kumar Sahoo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文