Simple Netty Server not sending response -


i'm writing custom protocol search service running on tcp. works perfect when using embeddedchannel testing. test futher, wrote server , add handlers up. request plain java socket client, server received data, process , send response. however, response not reach client socket. thought may have messed large channelpipeline. reduce implementation 1 inbound handler only. still doesn't work. can here?.

server:

public void start() throws exception{     eventloopgroup bossgroup = new nioeventloopgroup();     eventloopgroup workergroup = new nioeventloopgroup();      try {         final kaiexceptionhandler kaiexceptionhandler = new kaiexceptionhandler();         serverbootstrap b = new serverbootstrap();         b.group(bossgroup, workergroup)                 .channel(nioserversocketchannel.class)                 .childhandler(new channelinitializer<socketchannel>() {                     @override                     protected void initchannel(socketchannel socketchannel) throws exception {                         channelpipeline pipeline = socketchannel.pipeline();                         pipeline.addlast(new simplehandler());                     }                 });         channelfuture future = b.bind(new inetsocketaddress("localhost", 9400)).sync();         future.addlistener(new channelfuturelistener() {             @override             public void operationcomplete(channelfuture channelfuture) throws exception {                 if(channelfuture.issuccess()) {                     logger.info("kai server bounded '{}'", "localhost:9400");                 }else {                     logger.error("failed bound kai 'localhost:9400'", channelfuture.cause());                 }             }         });         future.channel().closefuture().sync();     }finally {         workergroup.shutdowngracefully();         bossgroup.shutdowngracefully();     } 

simple handler:

public class simplehandler extends channelinboundhandleradapter {  @override public void channelread(channelhandlercontext ctx, object msg) throws exception {     charset charset = charset.defaultcharset();     ctx.write(unpooled.copiedbuffer("client not seeing this", charset));     ctx.flush(); }  @override public void channelreadcomplete(channelhandlercontext ctx) throws exception {     ctx.flush(); } } 

test client. not-neat implementation. however, test.

public class testclient {   public static void main(string[] args) throws exception {      socket socket = new socket("localhost", 9400);     inputstream = socket.getinputstream();     stringbuilder sb = new stringbuilder();     byte[] buffer = new byte[64];     int r = 0;     socket.setsotimeout(10000);     system.out.println("reading...");     while ((r = is.read(buffer)) != -1) {         sb.append(new string(buffer).trim());     }     system.out.println("string: " + sb.tostring()); } 

}

your test program assuming connection gets closed after writing data, , assuming strings aren't fragmented in case of sending multibyte characters.

if wait till socket closure intentional, need change write statement following:

ctx.write(unpooled.copiedbuffer("client not seeing this", charset))     .addlistener(channelfuturelistener.close); 

this way of communication inefficient however, because need re-open socket connection everytime want server. best way making protocol either line based, or delimited length field, read responses line line in client using bufferedreader, , on server side, should add newline end of every message.

incase server needs receive message, should add new linebasedframedecoder() @ start of pipeline, followed optional new stringdecoder() let netty automatically turn bytebufs strings, you don't have anymore. netty can in reverse using stringencoder, can write string object, instead of wrapping every time in bytebuf.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -