title: Datagram socket asks for destination address
body: <p>When send a data using Datagram socket connection it fires exception: </p>
  
  <blockquote>
    <p>Destination address is null</p>
  </blockquote>
  
  <pre><code>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();
  </code></pre>
  
  <p>Why does it say that if UDP means broadcasting, so the receiver is everyone, no particular address?</p>
  
  <h1>What address must be used and where should it be placed?</h1>
  
  <p>Well I was using that code to get that address:</p>
  
  <pre><code>private InetAddress getBroadcastAdd() throws UnknownHostException {
      WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      DhcpInfo dhcp = wifi.getDhcpInfo();
      int broadcast = (dhcp.ipAddress &amp; dhcp.netmask) | ~dhcp.netmask;
      byte[] quads = new byte[4];
      for (int k = 0; k &lt; 4; k++)
          quads[k] = (byte) (broadcast &gt;&gt; (k * 8));
      return InetAddress.getByAddress(quads);
  }
  </code></pre>
  
  <p>But I still have the null Exception:</p>
  
  <p><a href="https://i.stack.imgur.com/UqQIe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UqQIe.png" alt="image"></a></p>