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

Categories

I am making a Client Server application for my Android phone.

I have created a UDP Server in Python which sits and listens for connections.

I can put either the server IP address in directly like 192.169.0.100 and it sends data fine. I can also put in 192.168.0.255 and it find the server on 192.169.0.100.

Is it possible to get the broadcast address of the network my Android phone is connected to? I am only ever going to use this application on my Wifi network or other Wifi networks.

Cheers

See Question&Answers more detail:os

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

1 Answer

From

http://code.google.com/p/boxeeremote/source/browse/trunk/Boxee+Remote/src/com/andrewchatham/Discoverer.java?spec=svn28&r=28

private InetAddress getBroadcastAddress() throws IOException {
    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));
    return InetAddress.getByAddress(quads);
}

This has the advantage of only looking at WIFI. I know OP said "I am only ever going to use this application on my Wifi network or other Wifi networks." but it's worth mentioning this in case someone else needs a non-wifi alternative.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...