Netty solving TCP sticky packet unpacking problems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.