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
This is one of the basic problems in distributed computing, and there are two approaches that work, to a degree:
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.
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.
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.
Either use multicast DNS (I do not know how you can use it on Java/Windows).
Or use IP broadcast (with UDP).
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.
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. 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.