我正在使用以下代码Hello, world!
使用 Java 发送到多播组。
InetSocketAddress sa = new InetSocketAddress("239.0.0.1", 12345);
MulticastSocket s = new MulticastSocket(sa);
s.setTimeToLive(255);
s.joinGroup(sa.getAddress());
byte[] sbuf = "Hello, world!".getBytes();
DatagramPacket sp = new DatagramPacket(sbuf, sbuf.length, sa);
s.send(sp);
byte[] rbuf = new byte[1024];
DatagramPacket rp = new DatagramPacket(rbuf, rbuf.length);
s.receive(rp);
System.out.format("Received \"%s\".\n", new String(rbuf));
s.leaveGroup(sa.getAddress());
s.close();
出于某种原因,代码总是抛出这样的异常:
java.io.Exception: Invalid argument
at java.net.PlainDatagramSocketImpl.send(Native Method)
at java.net.DatagramSocket.send(...)
at ...
为什么是这样?我该如何解决?