Netty Unpacking and Wrapping (iii)

Source: Internet
Author: User
Tags sendmsg

A mechanism for unpacking and gluing at the bottom of TCP programming

Split Package

The amount of data is sent multiple times when the amount of data sent is too large

Take the previous Helloword code as an example

 PackageCom.liqiang.nettyTest2; Public classNettymain { Public Static voidMain (string[] args) {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubServer Server =NewServer (8081);            Server.start ();        }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubClient client1=NewClient ("127.0.0.1", 8081);                Client1.connection (); Client1.sendmsg (In this chapter, we recommend Java Concurrency in practice by Brian Goetz. His book W "+" Ill give we ' ve reached an exciting point-in the next Chapter We ' ll discuss bootstr Apping, the process "+" of configuring and connecting all of Netty ' s components to bring your LE Arned about threading models on GE "+" neral and Netty ' s threading model in particular, whose per Formance and consistency Advantages we discuss "+" ed in detail on this chapter your general, we r Ecommend Java Concurrency in practice by Brian Goetz. Hi "+" s book would give we ' ve reached an exciting point-in the next Chapter We ' ll discuss bootstr Apping, the "+" process of configuring and connecting all of Netty ' s components to bring your LE Arned about Threading "+" models in general and Netty's threading model in particular, whose per FormAnce and consistency Advantag "+" es we discussed in Detailin this chapter your general, we recomm End Java Concurrency in practice by Bri "+" an Goetz. His book would give We ' ve reached an exciting point-in the next chapter;the counter is:1 2222 "+ "Sdsa ddasd asdsadas Dsadasdas");    }}). Start (); }}

Print

Can be found here split into 2 times sent

Sticky bag

When the amount of data sent is over an hour, it is combined into one send

 PackageCom.liqiang.nettyTest2; Public classNettymain { Public Static voidMain (string[] args) {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubServer Server =NewServer (8081);            Server.start ();        }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubClient client1=NewClient ("127.0.0.1", 8081);                Client1.connection ();  for(inti=0;i<100;i++) {client1.sendmsg ("D");    }}). Start (); }}

You can find that sometimes multiple sent data is combined into one send

Solution Solutions

Netty provides a decoder to solve the problem of unpacking and sticking the package

Linebasedframedecoder

To differentiate packages by line breaks

Server Side Add Linebasedframedecoder

 PackageCom.liqiang.nettyTest2;ImportIo.netty.channel.ChannelInitializer;ImportIo.netty.channel.socket.SocketChannel;ImportIo.netty.handler.codec.LineBasedFrameDecoder;ImportIo.netty.handler.codec.AsciiHeadersEncoder.NewlineType;ImportIo.netty.handler.codec.string.StringDecoder;ImportIo.netty.handler.codec.string.StringEncoder; Public classServerchannelinitializerextendsChannelinitializer<socketchannel> {    PrivateServer server;  Publicserverchannelinitializer (server server) { This. server=server; } @Overrideprotected voidInitchannel (Socketchannel Channel)throwsException {//TODO auto-generated Method Stubchannel.pipeline (). AddLast (NewLinebasedframedecoder (2048))//2048 is the maximum number of bytes to limit a package. If more than will be reported abnormal. AddLast ("Decoder",NewStringdecoder ())//receive data automatically convert buffer to string to avoid having to turn. AddLast ("encoder",NewStringencoder ())//send data can be sent directly inside the string frame converted to buffer transport. AddLast (Newserverhandle (server)); }}

Client

 PackageCom.liqiang.nettyTest2; Public classNettymain { Public Static voidMain (string[] args) {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubServer Server =NewServer (8081);            Server.start ();        }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubClient client1=NewClient ("127.0.0.1", 8081);                Client1.connection (); Client1.sendmsg (In this chapter, we recommend Java Concurrency in practice by Brian Goetz. His book W "+" Ill give we ' ve reached an exciting point-in the next Chapter We ' ll discuss bootstr Apping, the process "+" of configuring and connecting all of Netty ' s components to bring your LE Arned about threading models on GE "+" neral and Netty ' s threading model in particular, whose per Formance and consistency Advantages we discuss "+" ed in detail on this chapter your general, we r Ecommend Java Concurrency in practice by Brian Goetz. Hi "+" s book would give we ' ve reached an exciting point-in the next Chapter We ' ll discuss bootstr Apping, the "+" process of configuring and connecting all of Netty ' s components to bring your LE Arned about Threading "+" models in general and Netty's threading model in particular, whose per FormAnce and consistency Advantag "+" es we discussed in Detailin this chapter your general, we recomm End Java Concurrency in practice by Bri "+" an Goetz. His book would give We ' ve reached an exciting point-in the next chapter;the counter is:1 2222 "+ "Sdsa ddasd asdsadas Dsadasdas" +system.getproperty ("Line.separator"));//system.getproperty ("line.separator") compatibility between Linux and Windows}}). Start (); }}

If sending data forget to spell line break the server will not have any print

Delimiterbasedframedecoder

Differentiate by specifying symbols

Server-side

 PackageCom.liqiang.nettyTest2;Importio.netty.buffer.Unpooled;ImportIo.netty.channel.ChannelInitializer;ImportIo.netty.channel.socket.SocketChannel;ImportIo.netty.handler.codec.LineBasedFrameDecoder;ImportIo.netty.handler.codec.AsciiHeadersEncoder.NewlineType;ImportIo.netty.handler.codec.DelimiterBasedFrameDecoder;ImportIo.netty.handler.codec.string.StringDecoder;ImportIo.netty.handler.codec.string.StringEncoder; Public classServerchannelinitializerextendsChannelinitializer<socketchannel> {    PrivateServer server;  Publicserverchannelinitializer (server server) { This. server=server; } @Overrideprotected voidInitchannel (Socketchannel Channel)throwsException {//TODO auto-generated Method Stubchannel.pipeline (). AddLast (NewDelimiterbasedframedecoder (2048,unpooled.copiedbuffer ("$$__". GetBytes ())))//$__ is the delimiter for the package2048 is the maximum number of bytes to limit a package. If more than will be reported abnormal. AddLast ("Decoder",NewStringdecoder ())//receive data automatically convert buffer to string to avoid having to turn. AddLast ("encoder",NewStringencoder ())//send data can be sent directly inside the string frame converted to buffer transport. AddLast (Newserverhandle (server)); }}

Client

 PackageCom.liqiang.nettyTest2; Public classNettymain { Public Static voidMain (string[] args) {NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubServer Server =NewServer (8081);            Server.start ();        }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubClient client1=NewClient ("127.0.0.1", 8081);                Client1.connection (); Client1.sendmsg (In this chapter, we recommend Java Concurrency in practice by Brian Goetz. His book W "+" Ill give we ' ve reached an exciting point-in the next Chapter We ' ll discuss bootstr Apping, the process "+" of configuring and connecting all of Netty ' s components to bring your LE Arned about threading Models in GE "+" neral and Netty ' s threading model in$$__particular, whose performance and consistency advantages we discuss "+" ed in detail in this chap TER General, we recommend Java Concurrency in practice by Brian Goetz. Hi "+" s book would give we ' ve reached an exciting point-in the next Chapter We ' ll discuss bootstr Apping, the "+" process of configuring and connecting all$$__of Netty ' s components to bring your learned about threading "+" models in general and Netty ' s th Reading model in particular, whose performance and consistency Advantag ' + ' es we discussed in de Tailin This chapter all, we recommend Java Concurrency in practice by Bri "+" an Goetz. His book would give We ' ve reached an exciting point-in the next chapter;the counter is:1 2222 "+ "Sdsa ddasd Asdsadas Dsadasdas$__");    }}). Start (); }}

According to the $__ data was split into three packages

Netty Unpacking and Wrapping (iii)

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.