当我执行datagramChannel.joinGroup(multicastAddress,networkInterface)时,我得到UnsupportedOperationException;如果我做错了,请告诉我。我正在尝试使用NiodatagramChannel加入多播组。我不想使用OioDatagramChannel。 线程“main”java.lang.UnsupportedOperationException中的异常 在org.jboss.netty.channel.socket.nio.NioDatagramChannel.joinGroup(NioDatagramChannel.java:337) at io.netty.example.qotm.MulticastReceiver.run(MulticastReceiver.java:36) at io.netty.example.qotm.MulticastReceiver.main(MulticastReceiver.java:51)
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.channel.socket.DatagramChannel;
import org.jboss.netty.channel.socket.DatagramChannelFactory;
import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
public class MulticastReceiver {
private static ConnectionlessBootstrap connectionlessBootstrap;
public void run() {
DatagramChannelFactory datagramChannelFactory = new NioDatagramChannelFactory(
Executors.newCachedThreadPool());
connectionlessBootstrap = new ConnectionlessBootstrap(
datagramChannelFactory);
connectionlessBootstrap.setPipelineFactory(new ServerPipelineFactory());
connectionlessBootstrap.setOption("reuseAddress", "reuseAddress");
DatagramChannel datagramChannel = (DatagramChannel) connectionlessBootstrap
.bind(new InetSocketAddress(8888));
InetSocketAddress multicastAddress = new InetSocketAddress("224.2.2.3",
8888);
NetworkInterface networkInterface;
try {
networkInterface = NetworkInterface.getByInetAddress(InetAddress
.getByName("224.2.2.3"));
datagramChannel.joinGroup(multicastAddress, networkInterface);
// datagramChannel.connect(new InetSocketAddress("239.195.255.255",
// 8888));
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:0)
多播仅在使用java 7+时支持。好像你使用的是早期版本。我将改进错误信息,以便更清楚地了解原因。