Pregunta

I intend to create an Android app that should discover computers on LAN. I tried to use ping the broadcast address. I've got two problems:

  1. The determined broadcast address differs in Windows and on Android. Android tells me 10.255.255.255, http://www.remotemonitoringsystems.ca/broadcast.php tells me 10.111.111.255.
  2. When I try to use the broadcast from command prompt, I get no result on any of the 2 addresses

In Android, I use the following code to determine the address:

 InetAddress getBroadcastAddress()
  {
    try
    {
      WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      DhcpInfo dhcp = wifi.getDhcpInfo();
      // handle null somehow

      int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
      byte[] quads = new byte[4];
      for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
      return InetAddress.getByAddress(quads);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }

What address should I use? Why can't I get any ping from computers on the LAN? How should I determine the existing computers on the LAN?

¿Fue útil?

Solución

The approach was wrong.

Instead of pinging every existing computer, I should use the broadcast address to send out a specific message and receive reply from the computers that can read and understand that message.

That's how I eventually accomplished the task.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top