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

后端 未结 7 2236
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 02:36

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 compu

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 03:25

    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. 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. As M and N grow, the network traffic grows quadratically.

    • 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.

提交回复
热议问题