Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

networking - Network discovery in Java Multicast/Broadcast Java

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...