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: