简体   繁体   English

获取网络上可用的计算机的IP地址? -java

[英]get IP addresses of computer available on a network? -java

I am making a file sharing application which would look for computers which are running the application on the same network. 我正在创建一个文件共享应用程序,它将查找在同一网络上运行该应用程序的计算机。 So I would like my application to discover computers and their IP address. 所以我希望我的应用程序能够发现计算机及其IP地址。 Is this task achievable using Java? 使用Java可以实现此任务吗?

thanks 谢谢

This is one of the basic problems in distributed computing, and there are two approaches that work, to a degree: 这是分布式计算的基本问题之一,在某种程度上有两种方法可行:

Registry Service 注册服务

  • Somewhere on the network, you run a Registry Service with a well-known host and port number. 在网络上的某个位置,您使用众所周知的主机和端口号运行Registry Service。 This service has to be reachable / addressable from every place you want to run the application. 必须可以从要运行应用程序的每个位置访问/可寻址此服务。

  • On startup each instance of the application on the network registers itself with the registry. 启动时,网络上的每个应用程序实例都会向注册表注册。

  • When some machine / program needs to locate an instance of the application, it asks the registry. 当某些机器/程序需要找到应用程序的实例时,它会询问注册表。

Problems: 问题:

  • You have to deal with application instances that "go away" without telling the registry. 您必须在不告知注册表的情况下处理“消失”的应用程序实例。

  • You have to have a way to restore state if the registry restarts. 如果注册表重新启动,则必须有一种方法来恢复状态。

  • The applications have to know the name (or address) and port of the registry instance. 应用程序必须知道注册表实例的名称(或地址)和端口。

Broadcast / Multicast 广播/多播

  • Every instance of the application listens on a well-known "broadcast" or "multicast" address / port. 应用程序的每个实例都侦听一个众所周知的“广播”或“多播”地址/端口。

  • When a program wants to locate instances of the application, it sends a broadcast / multicast request. 当程序想要定位应用程序的实例时,它会发送广播/多播请求。

  • Each instance responds to the request giving its details. 每个实例都会响应请求,提供详细信息。

  • The program accumulates the responses to build a list of all "live" instances. 该程序累积响应以构建所有“实时”实例的列表。

Problems: 问题:

  • This doesn't scale. 这不规模。 Each and every request from M programs goes to N machines and generates N responses. 来自M个程序的每个请求都转到N台机器并生成N个响应。 As M and N grow, the network traffic grows quadratically. 随着M和N的增长,网络流量呈二次方式增长。

  • Broadcast and Multicast are lossy, especially on busy networks. 广播和组播是有损耗的,尤其是在繁忙的网络上。

  • Broadcast typically doesn't cross network boundaries. 广播通常不跨越网络边界。 Multicast requires special configuration. 组播需要特殊配置。


Either approach should work on a small network with a limited number of instances. 这两种方法都应该在具有有限数量实例的小型网络上运行。

The simple approach is to identify an existing distributed computing technology that does most of the work for you. 简单的方法是确定现有的分布式计算技术,为您完成大部分工作。 For example, RMI and a RMI registry, dynamic DNS, CORBA, JINI. 例如,RMI和RMI注册表,动态DNS,CORBA,JINI。

I found the answer to this question..I looked around and found this code. 我找到了这个问题的答案。我环顾四周,发现了这段代码。 It is not the best way to do it but it works.. 它不是最好的方法,但它的工作原理..

import java.io.IOException;
import java.net.InetAddress;

public class networkPing {

public static void main(String[] args) throws IOException {

    InetAddress localhost = InetAddress.getLocalHost();
    // this code assumes IPv4 is used
    byte[] ip = localhost.getAddress();

    for (int i = 1; i <= 254; i++)
    {
        ip[3] = (byte)i;
        InetAddress address = InetAddress.getByAddress(ip);
    if (address.isReachable(1000))
    {
        System.out.println(address + " machine is turned on and can be pinged");
    }
    else if (!address.getHostAddress().equals(address.getHostName()))
    {
        System.out.println(address + " machine is known in a DNS lookup");
    }

    }

}
}

Each computer should send DatagramPacket s to a multicast group. 每台计算机都应将DatagramPacket发送到组播组。 Also receive packets from the group. 还从组中接收数据包。 then use getAddress() to get InetAddress object from packet. 然后使用getAddress()从数据包中获取InetAddress对象。

Remember: to receive multicast packets, the system should join the multicast group. 切记:要接收组播数据包,系统应加入组播组。 But anyone (no need to join) can send packets to a multicast group. 但任何人(无需加入)都可以将数据包发送到多播组。

Example is here. 示例在这里。

I found this utility class part of Apache commons JCS (a caching library) particularly helpful, just copied it to my project since I didn't want/need to include the whole JCS library and the code is not available elsewhere separately (eg in apache commons-net would be nice): 我发现Apache commons JCS(一个缓存库)的这个实用程序类部分特别有帮助,只是将它复制到我的项目,因为我不想/需要包含整个JCS库,并且代码在其他地方不可用(例如在apache中) commons-net会很好):

You should take a look at this article on jxta . 你应该看看关于jxta的这篇文章 It's sun's P2P framework for Java and it's used by a ton of popular applications. 它是Sun的P2P Java框架,它被大量流行的应用程序所使用。 It might also be good to look at some applications that use jxta, because they may already do something like what you're trying to do. 查看一些使用jxta的应用程序可能也不错,因为它们可能已经做了类似于您尝试做的事情。

One problem here is that 'the same network' isn't well defined. 这里的一个问题是“同一网络”没有明确定义。 Do you mean a subnet? 你的意思是一个子网? All the nodes reachable prior to a router? 所有节点都可以在路由器之前到达?

If for example you mean 'the LAN', this has no meaning in TCP, but SAMBA might be of help. 例如,如果你的意思是“局域网”,这在TCP中没有意义,但SAMBA可能会有所帮助。

Some of that can be addressed by using appropriately scope multicast, if you can get the other nodes to respond. 如果可以让其他节点响应,可以使用适当的范围组播来解决其中一些问题。 Or if you know the subnet mask you can just do IP address arithmetic. 或者,如果您知道子网掩码,则可以执行IP地址算法。 But you need to define your problem more precisely first. 但是,您需要首先更准确地定义您的问题。

Either use multicast DNS (I do not know how you can use it on Java/Windows). 使用多播DNS(我不知道如何在Java / Windows上使用它)。

Or use IP broadcast (with UDP). 或使用IP广播(使用UDP)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM