java - Message sent from server not received by client when sending outside ServerHandler -
i trying send message netty server connected client.
what want similar this question. however, using io.netty (the 1 org.jboss.netty.* package) version 3.10.0.final
here have done :
telnetserver.java contains code initiate server.
public static void main(string[] args) throws exception { // configure ssl. final sslcontext sslctx; if (ssl) { selfsignedcertificate ssc = new selfsignedcertificate(); sslctx = sslcontext.newservercontext(ssc.certificate(), ssc.privatekey()); } else { sslctx = null; } // configure server. serverbootstrap bootstrap = new serverbootstrap( new nioserversocketchannelfactory( executors.newcachedthreadpool(), executors.newcachedthreadpool())); // configure pipeline factory. bootstrap.setpipelinefactory(new telnetserverpipelinefactory(sslctx)); // bind , start accept incoming connections. channel channel = bootstrap.bind(new inetsocketaddress(port)); }
telentserverpipelinefactory.java :
import org.jboss.netty.channel.channelpipeline; import org.jboss.netty.channel.channelpipelinefactory; import org.jboss.netty.channel.simplechannelupstreamhandler; import org.jboss.netty.handler.codec.frame.delimiterbasedframedecoder; import org.jboss.netty.handler.codec.frame.delimiters; import org.jboss.netty.handler.codec.string.stringdecoder; import org.jboss.netty.handler.codec.string.stringencoder; import org.jboss.netty.handler.ssl.sslcontext; import static org.jboss.netty.channel.channels.*; /** * creates newly configured {@link channelpipeline} new channel. */ public class telnetserverpipelinefactory implements channelpipelinefactory { private final sslcontext sslctx; public telnetserverpipelinefactory(sslcontext sslctx) { this.sslctx = sslctx; } public channelpipeline getpipeline() { // create default pipeline implementation. channelpipeline pipeline = pipeline(); if (sslctx != null) { pipeline.addlast("ssl", sslctx.newhandler()); } // add text line codec combination first, pipeline.addlast("framer", new delimiterbasedframedecoder( 8192, delimiters.linedelimiter())); pipeline.addlast("decoder", new stringdecoder()); pipeline.addlast("encoder", new stringencoder()); // , business logic. pipeline.addlast("handler", new telnetserverhandler()); pipeline.addlast("downstream", new telnetserveroutgoinghandler()); return pipeline; } }
and in telnetserverhandler.java, stored reference of channel when bounded:
public class telnetserverhandler extends simplechannelupstreamhandler { private static channel channel; public static channel getchannel() { return channel; } . . . . @override public void channelbound(channelhandlercontext ctx, channelstateevent e) throws exception { // todo auto-generated method stub channel = ctx.getchannel(); } }
i've tried send message outside telnetserverhandler using code :
scanner scanner = new scanner(system.in); system.out.printf("type message : "); message = scanner.nextline(); try{ if(channel.iswritable()){ channelfuture future = telnetserverhandler.getchannel().write(message); future.sync(); if(!future.issuccess()){ system.out.println("send failed : " + future.getcause()); } } }catch (exception e) { e.printstacktrace(); }
it prints no error. when debug, future.issucess() returns true value. client connected server. however, client never received message server.
did miss here? appreciated. in advance! :)
i've found solution.
change :
channelfuture future = telnetserverhandler.getchannel().write(message);
to :
channelfuture future = telnetserverhandler.getchannel().write(message+"\r\n");
this link points out needs line delimiter decoder read out message correctly.
hope helps. :)
Comments
Post a Comment