ClientBootstrap 的 connect 方法,好像在源代码里直接返回的就是:
@Override public ChannelFuture connect(SocketAddress remoteAddress) { return getUnsupportedOperationFuture(); }
是不是netty源代码有问题,这是从官方下载的。。。版本是:netty-3.4.2.Final 。这怎么用啊?
刚开始学,客户端代码是:
ChannelFactory channelFactory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ClientBootstrap cb = new ClientBootstrap(channelFactory); cb.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new ClientHandler()); } }); cb.setOption("tcpNoDelay", true); cb.setOption("receiveBufferSize", 1048576); cb.setOption("remoteAddress", new InetSocketAddress("localhost",5566)); ChannelFuture cf = cb.connect(); // Wait until the connection attempt succeeds or fails. Channel channel = cf.awaitUninterruptibly().getChannel(); if (!cf.isSuccess()) { cf.getCause().printStackTrace(); cb.releaseExternalResources(); return; }
到这一句ChannelFuture cf = cb.connect();就爆出:
java.lang.UnsupportedOperationException at org.jboss.netty.channel.AbstractChannel.getUnsupportedOperationFuture(AbstractChannel.java:137) at org.jboss.netty.channel.AbstractServerChannel.connect(AbstractServerChannel.java:55) at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:230) at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:183) at org.jboss.netty.bootstrap.ClientBootstrap.connect(ClientBootstrap.java:154) at client.Client.main(Client.java:30)
@javagg
UnsupportedOperationException
意思就是这堆东西有不支持,或写错的
cb.setOption(
"tcpNoDelay"
,
true
);
12
cb.setOption(
"receiveBufferSize"
,
1048576
);
13
cb.setOption(
"remoteAddress"
,
new
InetSocketAddress(
"localhost"
,
5566
));
一个一个试试吧