I'm trying to get a multicast socket working on a 'server' app, which will spit info at a bunch of android phones. The code snippet responsible for setting up the socket and sending a some data is as follows:
private MulticastSocket multisocket;
private DatagramPacket packet;
private InetAddress addr;
private Question question;
byte[] buffer = "Some text to be sent".getBytes();
packet = new DatagramPacket(buffer, buffer.length);
try {
addr = InetAddress.getByName("228.5.6.7");
multisocket = new MulticastSocket(4446);
multisocket.joinGroup(addr);
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println("Sending...");
multisocket.send(packet); // This is the line it dies on...
System.out.println("Text sent, closing socket");
multisocket.close();
} catch (IOException e) {
e.printStackTrace();
}
What happens is that it gets to the multisocket.send(packet);
line and dies with the following stack trace info:
Exception in thread "SendThread" java.lang.NullPointerException: null address || null buffer
at java.net.PlainDatagramSocketImpl.send(Native Method)
at java.net.DatagramSocket.send(Unknown Source)
at Model.QuestionSendThread.run(CommServer.java:158)
...and I am at a loss as to why.
One question I do have (and excuses the n00bishness of it) - Is the IP address you set in the multicast socket just a placeholder, or do you really have to have your IP address set to that? Half of me thinks you probably don't - the other half is screaming the opposite - but I can't find anything to confirm that when googling for an answer - just that it has to be an address in a fairly narrow range. If I've got this wrong (my IP is 192.168.1.3), then is that the problem? Or is it something else.
Thanks in advance
Steve