0

Here's what I'm trying to do- A server sends out "Alive message to all the PCs on the network and the PCs which are up and running, respond to the call by sending their IP.

I'm looking at a lightweight piece of coding as this will form a small bit of my application.

I've looked at Jini and other services but find that I may not need even half of their features(except for the network discovery)

Is it ok if I: 1. Use a for loop where a server opens a socket, checks(using a for loop) if all the IPs x.x.x.x are reachable by sending an "Alive" message. 2. On receiving the "alive" message at the client at the specific socket, the client replies with its IP.

Is this method OK? Do you think I could do it in a better way?

Thanks!

P R
  • 1,293
  • 7
  • 28
  • 58
  • Note- The server Sends out it's IP along with the Alive and the client knows the IP only after the server has sent it. I'm not planning to hardcode any IPs. thanks! – P R Dec 10 '11 at 10:12
  • A possible duplicate http://stackoverflow.com/questions/1158721/discovering-clients-on-a-wifi-network – GETah Dec 10 '11 at 10:15
  • Thanks! But the multicast UDP example assume that they know the client's IP. However, I need the network discovery to take place w/o the client's IP known to the Server. Broadcast sounds like a good option? – P R Dec 10 '11 at 10:27
  • @P Ramesh, yes a broadcast should be fine. Please see my answer below – GETah Dec 10 '11 at 10:49

1 Answers1

2

I had a similar problem a long time ago and I resolved it as follows:

  • The server broadcasts a UDP packet on the network to 255.255.255.255
  • All reachable clients will respond with a UDP packet that include their IP and any other information you wish to send.

The packet I personally used looks like

public class UDPDiscoveryPacket{
      public final long sendingTime;
      public final String clientIP;
      public UDPDiscoveryPacket(long sendingTime, String clientIP){
         this.sendingTime = sendingTime;
         this.clientIP = clientIP;
      }
}
GETah
  • 20,922
  • 7
  • 61
  • 103
  • @GETah- Yeah, I'll try that out. Meanwhile, I've modified a multicast UDP example where the Server sends it's IP the group of some random IP. all the clients part of that group respond with their IPs. – P R Dec 10 '11 at 11:56