java netty as tcpServer,delphi TIdTCPClient as tcpClient -


i use java netty tcpserver , delphi tidtcpclient tcpclient.the tcpclient can connect , send message tcpserver tcpclinet can not receive message sendback tcpserver . here tcpserver code written java:

public class nettyserver { public static void main(string[] args) throws interruptedexception {     eventloopgroup bossgroup = new nioeventloopgroup();     eventloopgroup workergroup = new nioeventloopgroup();     try {         serverbootstrap b = new serverbootstrap();         b.group(bossgroup, workergroup)                 .channel(nioserversocketchannel.class)                 .childhandler(new channelinitializer<socketchannel>() {                     @override                     public void initchannel(socketchannel ch)                             throws exception {                         ch.pipeline().addlast(new tcpserverhandler());                     }                 });         channelfuture f = b.bind(8080).sync();         f.channel().closefuture().sync();     } {         workergroup.shutdowngracefully();         bossgroup.shutdowngracefully();     } }   public class tcpserverhandler extends channelinboundhandleradapter { @override public void channelread(channelhandlercontext ctx, object msg) throws unsupportedencodingexception {     try {         bytebuf in = (bytebuf) msg;         system.out.println("channelread:" + in.tostring(charsetutil.utf_8));         byte[] responsebytearray = "hello".getbytes("utf-8");         bytebuf out = ctx.alloc().buffer(responsebytearray.length);         out.writebytes(responsebytearray);         ctx.writeandflush(out);         //ctx.write("hello");     } {         referencecountutil.release(msg);     } }  @override public void channelactive(channelhandlercontext ctx) throws  unsupportedencodingexception{     system.out.println("channelactive:" + ctx.channel().remoteaddress());     channelgroups.add(ctx.channel());  }  @override public void channelinactive(channelhandlercontext ctx) {     system.out.println("channelinactive:" + ctx.channel().remoteaddress());     channelgroups.discard(ctx.channel()); }  @override public void exceptioncaught(channelhandlercontext ctx, throwable cause) {     cause.printstacktrace();     ctx.close(); } 

}

here tcpclient code written delphi :

  astream := tstringstream.create;   idtcpclient.iohandler.readstream(astream); 

i use

idtcpclient.iohandler.readln() 

and still can not returndata.

your delphi code not match java code, why client not working.

the default parameters of tidiohandler.readstream() expect stream data preceeded stream length, in bytes, using either 32bit or 64bit integer in network byte order (big endian) depending on value of tidiohandler.largestream property. java code not sending array length before sending array bytes.

the default parameters of tidiohandler.readln() expect line data terminated either crlf or bare-ln terminator. java code not sending line terminator @ end of array bytes.

in short, java code not sending lets receiver know when sent data ends. unless closes connection after sending data, in case can set areaduntildisconnect parameter of tidiohandler.readstream() true, or use tidiohandler.alldata().

tcp stream-oriented, not message-oriented. sender must explicit message ends , next message begins.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -