TCP Sticky Pack Unpacking solution
1. Send a fixed-length message
Server side:
Eventloopgroup Pgroup = new Nioeventloopgroup (); Eventloopgroup CGroup = new Nioeventloopgroup (); Serverbootstrap B = new Serverbootstrap (); B.group (Pgroup, CGroup). Channel (Nioserversocketchannel.class). Childhandler (New channelinitializer<socketchannel> () {@Overrideprotected void Initchannel (Socketchannel SC) Throws Exception {//sets the fixed-length string to receive Sc.pipeline (). AddLast (New Fixedlengthframedecoder (5));//sets the decoding Sc.pipeline () as a string. AddLast (New Stringdecoder ()); Sc.pipeline (). AddLast (New Serverhandler ());}); Channelfuture CF = B.bind (8765). sync (); Cf.channel (). Closefuture (). Sync ();p group.shutdowngracefully (); Cgroup.shutdowngracefully ();
Client
Eventloopgroup Group = new Nioeventloopgroup (); Bootstrap B = new Bootstrap (); B.group (group). Channel (Niosocketchannel.class). Handler (New channelinitializer< Socketchannel> () {@Overrideprotected void Initchannel (Socketchannel sc) throws Exception {Sc.pipeline (). AddLast ( New Fixedlengthframedecoder (5)); Sc.pipeline (). AddLast (New Stringdecoder ()); Sc.pipeline (). AddLast (New ClientHandler ());}); Channelfuture CF = B.connect ("127.0.0.1", 8765). sync (); Cf.channel (). Writeandflush (Unpooled.wrappedbuffer (" 111111112222222 ". GetBytes ())); Cf.channel (). Writeandflush (Unpooled.copiedbuffer (" 3333333 ". GetBytes ())); Cf.channel (). Closefuture (). sync (); group.shutdowngracefully ();
2. Split with special delimiter
Server side:
Eventloopgroup pgroup = new nioeventloopgroup (); Eventloopgroup cgroup = new nioeventloopgroup (); Serverbootstrap b = new serverbootstrap (); B.group (Pgroup, cgroup) .channel ( Nioserversocketchannel.class) .option (channeloption.so_backlog, 1024) .option (ChannelOption.SO_ sndbuf, 32*1024) .option (channeloption.so_rcvbuf, 32*1024) .childhandler (new Channelinitializer<socketchannel> () {@Overrideprotected void initchannel (socketchannel &NBSP;SC) throws exception {//set the special delimiter Bytebuf buf = unpooled.copiedbuffer ("$_". GetBytes ()); Sc.pipeline (). AddLast (New delimiterbasedframedecoder (1024,&NBSP;BUF));//Set decoding in string form Sc.pipeline (). AddLast (New stringdecoder ()); Sc.pipeline (). AddLast (New serverhandler ());}); Channelfuture cf&nbsP;= b.bind (8765). sync (); Cf.channel (). Closefuture (). Sync ();p group.shutdowngracefully (); Cgroup.shutdowngracefully ();
client:
eventloopgroup group = new nioeventloopgroup (); Bootstrap b = new bootstrap (); B.group (group) .channel (Niosocketchannel.class) . Handler (new channelinitializer<socketchannel> () {@Overrideprotected void initchannel (SOCKETCHANNEL&NBSP;SC) throws exception {//$_ as a split symbol bytebuf buf = Unpooled.copiedbuffer ("$_". GetBytes ()); Sc.pipeline (). AddLast (New delimiterbasedframedecoder (1024, BUF)); Sc.pipeline (). AddLast (New stringdecoder ()); Sc.pipeline (). AddLast (New clienthandler ());}); Channelfuture cf = b.connect ("127.0.0.1", 8765). sync (); Cf.channel (). Writeandflush ( Unpooled.wrappedbuffer ("111111111$_". GetBytes ())); Cf.channel (). Writeandflush (Unpooled.wrappedbuffer ("222$_". GetBytes ()));//waits for the client port to close Cf.channel (). Closefuture (). sync (); group.shutdowngracefully ();
Adopt a custom protocol approach
Netty solving TCP sticky packet unpacking problems