Network discovery in Java Multicast/Broadcast Java

百般思念 提交于 2019-11-28 02:19:48

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/8455755/network-discovery-in-java-multicast-broadcast-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!