简体   繁体   中英

Send string without “\n” in Netty

I have a simple socket client written in netty.io and for data sending and receiving I use SimpleChannelInboundHandler . and in Initializer class I defined this:

pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));

but in this way I need every time put "\\n" new line symbol at the end of string which I needn't. without this symbol I can't send or receive any string data. what is the solution use string data in netty without new line delimiter?

You can specify whatever delimiter you like, for instance

ByteBuf[] delimiters = new ByteBuf[] {
    Unpooled.wrappedBuffer(new byte[] { '\r', '\n' }), // WINDOWS
    Unpooled.wrappedBuffer(new byte[] { '\n' }),       // UNIX / OSX
    Unpooled.wrappedBuffer(new byte[] { '\r' })        // LEGACY MAC
};
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(MAXFRAMELENGTH, delimiters));

In this way, if you omit it, you can get rid of \\n and replace with another character.

If you want to omit any delimiter, than I think you could use a FixedLengthFrameDecoder : pipeline.addLast("framer", new FixedLengthFrameDecoder(yourFrameLength)); or a LengthFieldBasedFrameDecoder for more options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM