开发者

Java MulticastSocket causing null pointer exception

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 Multi开发者_如何学JAVAcastSocket 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


MulticastSocket and its joinGroup() are about receiving multicast packets. Sending multicast packets is the same as sending regular UDP packets to the address of group, therefore you need to set destination address and port for each packet:

packet = new DatagramPacket(buffer, buffer.length, group, 4446); 
...
multisocket.send(packet);

See also:

  • MulticastSocket


What he said. In fact you don't need a MulticastSocket or joinGroup() at all to send: you can do it with a regular DatagramSocket. But you do have to put the destination address into the DatagramPacket. Also your exception handling is up the pole. If you get an exception in the first try block you are still proceeding to the second one. All this code should be inside a single try block.


You can also receive the same exception java.lang.NullPointerException: null buffer || null address from send calls if the InetSocketAddress written into DatagramPacket.setAddress(...) was unresolved or badly formed.

For instance this looks plausible given the usual InetAddress.toString() format, but isn't:

DatagramPacket.setAddress(new InetSocketAddress("/239.193.129.14", 3450))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜