1

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. Is this task achievable using Java?

thanks

Tushar Chutani
  • 1,522
  • 5
  • 27
  • 57
  • You can create a common place, like a server where all network computer updates their current ip, like an ip pool. That would be more secure too! And other computers can get available ip from pool. – TeaCupApp Aug 27 '11 at 01:24
  • I agree but I would like my application to do the all the work and not be dependent on any external device/software – Tushar Chutani Aug 27 '11 at 01:26
  • Well, What you can do, But again it's not efficient! You can do a peer to peer networking. And each available computer make other networked computer awards of their presence. Way too much work in that way. Why not server? I mean even messenger uses fusion! you check who is online and than you connect peer to peer for chat and file sharing. – TeaCupApp Aug 27 '11 at 01:31

7 Answers7

3

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • +1 for broadcast. Given the questioners stated (and very limited) scope and requirements, it should achieve the desired functionality. – pap Aug 29 '11 at 09:40
1

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");
    }

    }

}
}
Tushar Chutani
  • 1,522
  • 5
  • 27
  • 57
  • Note: that is a simplified approach that assumes a /24 subnet mask (255.255.255.0). If you're on a network with a different subnet setup, your "local address" range might not be as simple as "1-254". – pap Aug 29 '11 at 09:38
0

Each computer should send DatagramPacket s to a multicast group. Also receive packets from the group. then use getAddress() to get InetAddress object from packet.

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.

Mohammed Shareef C
  • 3,829
  • 25
  • 35
0

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 (e.g. in apache commons-net would be nice):

Gregor
  • 1,297
  • 1
  • 19
  • 31
0

You should take a look at this article on jxta. It's sun's P2P framework for Java and it's used by a ton of popular applications. 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.

Jon7
  • 7,165
  • 2
  • 33
  • 39
  • Links to external sites without any additional information aren't answers, and shouldn't be posted as such. Answers should stand on their own, so that they remain useful and searchable. Posting a link to additional information is fine, but posting nothing but a link should be a comment. Following the guidelines helps keep this site useful and uncluttered. – Ken White Aug 27 '11 at 01:45
  • @Ken thanks for the reminder. I got distracted and must have submitted before I was done! – Jon7 Aug 27 '11 at 04:26
0

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.

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. But you need to define your problem more precisely first.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Either use multicast DNS (I do not know how you can use it on Java/Windows).

Or use IP broadcast (with UDP).

ysdx
  • 8,889
  • 1
  • 38
  • 51