Java MulticastSocket 导致空指针异常
我正在尝试让多播套接字在“服务器”应用程序上工作,该应用程序将向一堆 Android 手机发送信息。负责设置套接字并发送一些数据的代码片段如下:
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();
}
发生的情况是,它到达 multisocket.send(packet);
行并终止,并显示以下堆栈跟踪信息:
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)
...我不知道为什么。
我确实有一个问题(请原谅它的无用性) - 您在多播套接字中设置的 IP 地址只是一个占位符,还是您真的必须将您的 IP 地址设置为该地址?我一半认为你可能不知道 - 另一半则尖叫着相反 - 但我在谷歌搜索答案时找不到任何东西来证实这一点 - 只是它必须是一个相当窄范围内的地址。如果我错了(我的IP是192.168.1.3),那么这就是问题所在吗?或者是别的东西。
预先感谢
史蒂夫
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MulticastSocket
及其joinGroup()
用于接收多播数据包。发送组播数据包与发送常规 UDP 数据包到组地址相同,因此您需要为每个数据包设置目标地址和端口:另请参阅:
MulticastSocket
MulticastSocket
and itsjoinGroup()
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:See also:
MulticastSocket
他说什么。事实上,您根本不需要 MulticastSocket 或 joinGroup() 来发送:您可以使用常规 DatagramSocket 来完成。但您必须将目标地址放入 DatagramPacket 中。此外,您的异常处理也很出色。如果您在第一个 try 块中遇到异常,您仍然会继续执行第二个 try 块。所有这些代码都应该位于一个单个 try 块内。
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.
您还可以收到相同的异常
java.lang.NullPointerException: null buffer ||如果写入
。DatagramPacket.setAddress(...)
的InetSocketAddress
未解析或格式错误,则来自发送调用的 null 地址例如,考虑到通常的
InetAddress.toString()
格式,这看起来似乎合理,但事实并非如此:You can also receive the same exception
java.lang.NullPointerException: null buffer || null address
from send calls if theInetSocketAddress
written intoDatagramPacket.setAddress(...)
was unresolved or badly formed.For instance this looks plausible given the usual
InetAddress.toString()
format, but isn't: