1

When send a data using Datagram socket connection it fires exception:

Destination address is null

DatagramSocket ds = new DatagramSocket(1050);
                ds.setBroadcast(true);
                InetAddress broadcastAdress = getBroadcastAdd();
                DatagramPacket pack = new DatagramPacket(data, data.length, broadcastAdress, 1050);
                ds.send(pack);
                ds.close();

Why does it say that if UDP means broadcasting, so the receiver is everyone, no particular address?

What address must be used and where should it be placed?

Well I was using that code to get that address:

private InetAddress getBroadcastAdd() throws UnknownHostException {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    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);
}

But I still have the null Exception:

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • UDP does *not* mean broadcasting, and even if you are broadcasting you still have to (a) set broadcast mode on the socket and (b) provide a destination broadcast address in the datagram. Unclear what you're asking. – user207421 Aug 03 '17 at 08:23
  • Possible duplicate of [Android Broadcast Address](https://stackoverflow.com/questions/2993874/android-broadcast-address) – Cédric Julien Aug 03 '17 at 08:57
  • The image clearly shows that you are not getting a null exception, but rather a [`NetworkOnMainThreadException`](https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html) exception: "*The exception that is thrown when an application attempts to perform a networking operation on its main thread.*" You can't perform network operations in the main thread, you need to use a worker thread or asynchronous task instead. – Remy Lebeau Aug 03 '17 at 18:58

1 Answers1

2

If you want to use UDP to broadcast a message, then you should use the setBroadcast on your socket, and use the broadcast address (an address is always needed, even in case of broadcasting).

DatagramSocket udpSocket = new DatagramSocket(1050);
udpSocket.setBroadcast(true);
InetAddress broadcastAdress = getAdressBroadcast();
DatagramPacket pack = new DatagramPacket(data, data.length, broadcastAdress, 1050);
udpSocket.send(pack);

With the getAddressBroadcast like this :

public static String getAdressBroadcast() throws SocketException {
    System.setProperty("java.net.preferIPv4Stack", "true");
    for (Enumeration<NetworkInterface> networkInterfaceEnum = NetworkInterface.getNetworkInterfaces(); networkInterfaceEnum.hasMoreElements();) {
        NetworkInterface networkInterface = networkInterfaceEnum.nextElement();
        if (!networkInterface.isLoopback()) {
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                return interfaceAddress.getBroadcast().toString().substring(1);
            }
        }
    }
    return null;
}

As a side note, do not forgot to do those action (networking operation) should not be done in the main activity thread, but in an Async task

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • 1
    There is no 'otherwise' about it. An address is always needed, whether unicast, broadcast, or multicast. Your own code exhibits that. – user207421 Aug 03 '17 at 08:26
  • Hello.. what is getAddressBroadcast? –  Aug 03 '17 at 08:46
  • would it be just InetAddress.getByName("0.0.0.0"); ? –  Aug 03 '17 at 08:55
  • @AlisherBasmati : I've updated my answer, but check this question : https://stackoverflow.com/questions/2993874/android-broadcast-address my code was originally inspired by it. – Cédric Julien Aug 03 '17 at 08:58
  • @AlisherBasmati No. Can you possibly look up what a broadcast address actually is? Instead of all this guessing? – user207421 Aug 03 '17 at 08:59
  • ok, I also updated Q. why I used 0.0;0;0 because in .NET in would be just any address –  Aug 03 '17 at 09:35
  • 1
    Cedric, if you add this reference into your answer I will mark it as Answer. Also, pls, write that the Networking Operation should be performed in Async way, not is the main Activity form. The reference: https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception –  Aug 03 '17 at 09:54
  • 1
    @AlisherBasmati I've added a reference to Async task. – Cédric Julien Aug 03 '17 at 10:17