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
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
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.