例外:netty 3.10.6的套接字客户端的java.nio.channels.NotYetConnectedException [英] EXCEPTION: java.nio.channels.NotYetConnectedException for a socket client with netty 3.10.6
问题描述
我需要连接到服务器,并且遵循以下代码:
i need to connect to a server and i'm following the code below for same:
package SocketServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.channels.Channel;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
public class SocketClient {
final static Logger logger = Logger.getLogger(SocketClient.class);
public static void write(String bscsString){
String HOST = PropertyUtils.getProperty("HOST");
int PORT = Integer.parseInt(System.getProperty("port", PropertyUtils.getProperty("PORT")));
//create a Boss thread from thread pool
Executor BossPool = Executors.newCachedThreadPool();
Executor WorkerPool = Executors.newCachedThreadPool();
//create NIO client channel factories
ChannelFactory channelFactory = new NioClientSocketChannelFactory(BossPool,WorkerPool);
//Client Setup the channel using bootstrap
ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory);
//setup the Channel PipeLine Factory
ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
new ObjectEncoder()
);
}
};
//set the pipeline
clientBootstrap.setPipelineFactory(pipelineFactory);
//connect to host port
InetSocketAddress addressToConnectTo = new InetSocketAddress(HOST, PORT);
//connect asynchronously :)
ChannelFuture cf = clientBootstrap.connect(addressToConnectTo);
//get the channel to send message
final org.jboss.netty.channel.Channel channel = cf.getChannel(); //could be a error here
String s = "hello from client";
ChannelFuture test = channel.write(s);
System.out.println("Written on server "+test);
test.syncUninterruptibly();
System.out.println("client synced "); }
}
但是,我遇到以下错误:
However, i'm getting the following error:
Nov 23, 2016 8:41:28 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d] OPEN
2016-11-23 20:41:28 INFO SocketClient:60 - chanelfuture for Written on server org.jboss.netty.channel.DefaultChannelFuture@2f6a9cd7
java.nio.channels.NotYetConnectedException
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:431)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:128)
at org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.eventSunk(NioClientSocketPipelineSink.java:84)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendDownstream(DefaultChannelPipeline.java:779)
at org.jboss.netty.channel.Channels.write(Channels.java:725)
at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.doEncode(OneToOneEncoder.java:71)
at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:59)
at org.jboss.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:591)
at org.jboss.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:582)
at org.jboss.netty.channel.Channels.write(Channels.java:704)
at org.jboss.netty.channel.Channels.write(Channels.java:671)
at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:347)
at SocketServer.SocketClient.write(SocketClient.java:59)
at SocketServer.SocketServerHandler.channelReadComplete(SocketServerHandler.java:58)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelReadComplete(AbstractChannelHandlerContext.java:408)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelReadComplete(AbstractChannelHandlerContext.java:390)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelReadComplete(AbstractChannelHandlerContext.java:383)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelReadComplete(DefaultChannelPipeline.java:1339)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelReadComplete(AbstractChannelHandlerContext.java:408)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelReadComplete(AbstractChannelHandlerContext.java:390)
at io.netty.channel.DefaultChannelPipeline.fireChannelReadComplete(DefaultChannelPipeline.java:932)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:571)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:512)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:426)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:398)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:877)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
at java.lang.Thread.run(Thread.java:745)
Nov 23, 2016 8:41:28 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d] EXCEPTION: java.nio.channels.NotYetConnectedException
Nov 23, 2016 8:41:28 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d, /10.0.41.160:45707 => /10.0.41.160:11011] BOUND: /10.0.41.160:45707
Nov 23, 2016 8:41:28 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d, /10.0.41.160:45707 => /10.0.41.160:11011] CONNECTED: /10.0.41.160:11011
Nov 23, 2016 8:41:48 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d, /10.0.41.160:45707 :> /10.0.41.160:11011] DISCONNECTED
Nov 23, 2016 8:41:48 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d, /10.0.41.160:45707 :> /10.0.41.160:11011] UNBOUND
Nov 23, 2016 8:41:48 PM org.jboss.netty.channel.DefaultChannelPipeline
WARNING: The pipeline contains no upstream handlers; discarding: [id: 0xa0193d7d, /10.0.41.160:45707 :> /10.0.41.160:11011] CLOSED
我只使用过netty,所以这有点误导. 似乎在写入服务器时出现问题,但是会引发尚未连接的异常
i've worked with netty a bit only so this one is a bit misleading. it seems it has issues while writing to server, however it throws not yet connected exception
在这个问题上,请指导我.谢谢
Please guide me in this issue. Thanks
推荐答案
您不想调用cf.getChannel,因为这会给您带来过早的频道.相反,您应该将监听器添加到将来,一旦建立连接便会触发该监听器.
You don't want to call cf.getChannel as that gives you the channel too soon. Instead you should add a listener to the future that will be triggered once the connection has been made.
这篇关于例外:netty 3.10.6的套接字客户端的java.nio.channels.NotYetConnectedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!